title: How to decode PDF stream to downloadable file with PHP?
body: <p>I am running a PHP script that extracts a PDF file from a website. Everything works, but my problem is that once I get the PDF file, I don't know how to convert it into a downloadable file. Here is the output I get from my script:</p>
  
  <pre><code>%PDF-1.6
  5 0 obj
  &lt;&lt;
  /Type /Page
  /Parent 3 0 R
  /Resources 4 0 R
  /Contents 6 0 R
  /MediaBox[ 0 0 612.0 792.0 ]
  /CropBox[ 0 0 612.0 792.0 ]
  /Rotate 0
  &gt;&gt;
  endobj
  6 0 obj
  &lt;&lt; /Length 4407 /Filter /FlateDecode &gt;&gt;
  stream
  x��[ms�H��������[u�*[V�Z-��0&amp;   ;��̮omɠ��b$���}NK�q���7�����s�&gt;oRlf������f���WW�ׯ��ƙ�Z�e���������7��+�m6���U�:̣K�l�B�as�������-.�ܱ|���EA�N}��!�a���`�Vs�C,W2O���1���,��Gڕ~�$�͓pOXA�3�������
  &amp;��t|�w����׉��q����tF�:�4�.�嚋�n�(��&lt;
  7�6Is��`j+���b}w����|��|�f�mo&gt;��jɦI�d�d�?�P�@Y�sJƎEP��l�zq�|1�a䋿��6I��F���l�7����w`aW幖R��;�G '��s���s�^���E~�n�'|��"���ׯޛ6ul�����X��o�W�u�(��߃�؁��o9�)���_�E��{9Ƣ���7&gt;g��ߍ�³���"��d�S�eǒ�ep�&lt;��?���������V�p,[} W\��_����ɻq4����w滶tZ���nX�
  \y��f���E��ҝB�#�k`U:h����Ip�oF��,��@�Z�W%�i��ݜ��u�o����}%�\�[�/N"��E�I�Z&gt;�ˈ���X��&lt;�K0)���   G8��x�'�3�~�6����qe�-[���z�+�P�T_~�Vav��1�&lt;h��R*�B�^�]��a��VG�a~&lt;���xp�^ږ氁)�oW���W+�|d�p    ��F��}��Kָ9qWJn�r[L�n)@�����r�J�5�~w��GVl�*w,Q��iC
  (v$.&amp;mQ��gy��"��+���(XS���¨���D����=�.BI��U�6��JN�h�S��I�����e�H%��jۗ��R(��/�M d-�e�I���1g��0}��&lt;J��%���!�hpq\&gt;�\�K�B@
  �݆��?��42=u⎳�fE�ěe��-Y�?F�S�E�݄3sep3�w��&lt;'Ib��b�
  ��)��Y��YAP�܄@S��ry�[��M��8daO��3�6�(]=��Ҽ�x��4(}K�nKq{�/��%�
  ��@��@��Z�Qt��"0^��r5���% �U�S�E��\dVp��s�n�s���s���T�9#㇣�@Ֆ�L��Aw���|��7G�7�do���Ygy�YDgl���,b8j@�W��g$�|n�
  ;AN�6�7��g���t��D��wnK%�H��T��s�es  ���VY��s�#,�P��et�ċO�mC�#
  �XU
  }�����IҝL����L���ǖ%#S!��`4�鏐r���_��������E�i,����¯"�}U.B�%q%o+���7�i��?�\��������KV);   ��s+�@i���gq
  �A������vf�Ý���u�,�2��E��AJm�"���I��O�&amp;�2x�vti,C���β7�lE��r$�N  ޫ��
  ��JW֯��cBG ��Q�h(�NKY�@�'�$�M٢����  �.�碀J�zD���h?�j� ��m3u  �PM~d
  ��OC    ��wA_�lq��6��������!�i��z�
  ﺣ�a�ɨ��@��U��
  zn"����cp�L�^�q)��Az���2�unuma�C�t�ַ�(C�;���Q��n�p����S�    �qSU�_�Ë�z��d��&amp;���������2�cd=i��.��˶�"�U�8���d�oJ�h(ϣ
  �!�am�E���*Ā0[%O��z���&gt;bY��a�   ��m��&gt;�K��h���Ys��7�:Y�1by���ux��$)ҎA�d��,�f�L�u"�G���͛^�������5����4���!��f�u
  
  ��������F�
  �u������o9��ᚍUG���(�%V�T}Z��=&gt;)�'�cR0��$�ް;3�
  n�˱=W��D���Pc���V��5t��)�F0-E��
  5��� �H�y*$�t�5~��*�f��'�,��
  ����l�7�J������s�R�]�~b�c6ϛ�+x¥���7�y���i��!ܼl�H�%��/S�iO�
  1�+���9;���!L1�G��H��#Lǰ].Z��є)Mal_����v�dP��d�Lm�P����
  (&lt;�}��� D�m���Z����۔��Ή�M,���Lе!��=� 1��[$v}^x���K畅j\��ǈ�(zY��e��Pnr��9\�"vƲ�x��Ң�.�N��3�I��cL��3E���YC���c���h��!���N��b�bSx�m�`�������P5�_ "���BqU��g�������)OM�D��$9��\`c�m��Bǎ��MF��_���-�ϣc$;���L�]i����\�7�f�u�ۘRz¡�q�����ퟢ60 j���C����n��IE�z[��])�#ǽ���մ��M�o1�4�-���Ȕ}Y��R�L��e�FZ�&lt;N��&amp;!����ߦf�dU��i��s���.��:`����],�mޯ"j�3]&gt;��kܺ1�hR���ٯ��'�x�Y ��-&gt;�ڻ6�
  gvp�(�J?��O�Vb~�l�JuBG7L�ɺy��PjQ�D$��p��r�n��\%�"OMV�|y��o�9���hZ�9�rT�nUFL�u��j��I�g+q�&lt;��~��n���E�H�Ѽ,�o�M����2J�"]��}��˳(p�B�6�h�/��q=�jʖ��*Pg�����ô�.&amp;��/&amp;�;o��\8�[=1�:�(q6��*Z&gt;�zk��\����t!���l9�[�?�U�g�Y��`Q^�E�(}�6�g�Cy���7� �2��7\*��5�'��u�D|��+�^H���E��#�\#�=l������e���}�|��KF̥&lt;a�,v�ª��=Ƌ��vި�xU�W�xe�4����&gt;�����Ǉ$�pH���Y+w8!^;ZZ��P�SF/����M��z-{���\�����&gt;G��_�ʲ�cs�V�ֈ�,�������8'�!�&gt;LQڟ��.�_X�s3�F�1))�@Y�Ԙ*�C'��-H��"�x9�-ys�%}t)Y/��QB����&gt;^�0�*F�+l��U�e:3׸d����,�R����p�]!�_��&gt;G[N�RٯZ���ճ�����:����:���J#��u��,���UF4����_��Hjw���9�w���s���$p�H2�
  �GI�&gt;�"��i��3�&amp;�q����(�T�hۮΫ���mq5���&gt;ʟ�����n���Rc\���
  �83=����Ծ6�Xz�u��o?��4�DWh�K�
  _::�a�|���:��*���De�O#\BD���9�x�!�nU�m^Aeo�_P�:��
  �I�����&gt;v�`��b�{�   `���������/ȝ�o�5�?�q�*ʘ#��gU�*
  `�E��T������C3옝��WB�8Z��m��E�!�h���cvƎx"o   �w�:�b�{ftn}ԃ�j�o����W�;��-R]}?�|n�0B1�:P���'���#�����螘v�����(�t������;^A��1pi&amp;�T�^�ć'�TU��?���(1���R�'f�?(����l�8�U�'R�H�7��q�S�@6�t'�"���
  l��'�W�-���%�_Cq���+cԣ��h��a���[�i���۰��+()���W�lVT��IEӕ�A�)��-�C��QC�(N]��q8�~���!EM��)=q���������L��  dh�;rm�������7!\����o��:tJ�C�6Z�nI����T{�[$�ٯ�2�\��=����&gt;����x�2
  �!k���(�$SM��G
  ��ö��6�}F�G;��t��$F=vxq���+z��_㶸W�&amp;³�;��V�n�g�Z����4�u.&lt;��x�~V��+KR���&amp;p�}�����Ջ�T�'�"�J��A�_���0�Q1��D��A�����q����;5I/
  p[�p��8)tH�m����6:'DW�@2�~�TR@]S��T6    �'��Ҍ�(��f*{�Tk������@W��մ��:���S�U N�Q��=g_I�!4K*��4��6��Q�K�S䚹Đ���]���έ����!���2�'��*�['�'[��J-�Ҩ�]��d݁��3��-�2�΍~��s@��-��K���sѕP��������l��]Erώ���#.�V� D�����/��vU���[Yf����&amp;kS�&amp;&gt;7���y��V|}��th��S    �i�E'����Jޏ��^@/��A��6���e+*
  Z��
  ��c�s��
  ���M?�  a���pK�N�jwUi���atU��#���ї���a%;
  endstream
  endobj
  1 0 obj
  &lt;&lt;
  /Type /Catalog
  /Pages 3 0 R
  &gt;&gt;
  endobj
  2 0 obj
  &lt;&lt;
  /Type /Info
  /Producer (Oracle BI Publisher 11.1.1.9.0)
  &gt;&gt;
  endobj
  3 0 obj
  &lt;&lt;
  /Type /Pages
  /Kids [
  5 0 R
  ]
  /Count 1
  &gt;&gt;
  endobj
  4 0 obj
  &lt;&lt;
  /ProcSet [ /PDF /Text ]
  /Font &lt;&lt; 
  /F1 7 0 R
  /F2 8 0 R
  /F3 9 0 R
  &gt;&gt;
  &gt;&gt;
  endobj
  7 0 obj
  &lt;&lt;
  /Type /Font
  /Subtype /Type1
  /BaseFont /Helvetica
  /Encoding /WinAnsiEncoding
  &gt;&gt;
  endobj
  8 0 obj
  &lt;&lt;
  /Type /Font
  /Subtype /Type1
  /BaseFont /Helvetica-Bold
  /Encoding /WinAnsiEncoding
  &gt;&gt;
  endobj
  9 0 obj
  &lt;&lt;
  /Type /Font
  /Subtype /Type1
  /BaseFont /Helvetica-Oblique
  /Encoding /WinAnsiEncoding
  &gt;&gt;
  endobj
  10 0 obj
  [ 5 0 R /XYZ 48.6 121.263 null ]
  endobj
  11 0 obj
  [ 5 0 R /XYZ 48.6 121.263 null ]
  endobj
  xref
  0 12
  0000000000 65535 f
  0000004654 00000 n
  0000004709 00000 n
  0000004791 00000 n
  0000004859 00000 n
  0000000010 00000 n
  0000000168 00000 n
  0000004957 00000 n
  0000005062 00000 n
  0000005172 00000 n
  0000005285 00000 n
  0000005337 00000 n
  trailer
  &lt;&lt;
  /Size 12
  /Root 1 0 R
  /Info 2 0 R
  /ID [&lt;b6e546c805d4be27e2c7679a536a9863&gt;&lt;b6e546c805d4be27e2c7679a536a9863&gt;]
  &gt;&gt;
  startxref
  5389
  %%EOF
  </code></pre>
  
  <p>I have tried searching for examples of PHP scripts where the stream string is decoded, but can't seem to find much. Does anyone know how to do this? </p>
  
  <p>****Update****</p>
  
  <p>Here is the code that I'm, using to get the PDF file:</p>
  
  <pre><code>$bol = '104927876';
  
          $loginUrl = 'https://speedship.wwex.com/pls/apex/f?p=77055:101:104062064171792';
          $remotePageUrl = "https://speedship.wwex.com/pls/apex/f?p=77055:101:104062064171792:PRINT_REPORT=Bill_Of_lading:::GEN_VAL1,GEN_VAL2:$bol,N"; 
          $loginFields = array(
                          'p_t01'=&gt;'username', 
                          'p_t02'=&gt;'password',
                          'p_flow_id' =&gt; '77055',
                          'p_flow_step_id' =&gt; '101',
                          'p_instance' =&gt; '104062064171792',
                          'p_page_submission_id' =&gt; '4717306236799',
                          'p_request' =&gt; '',
                          'p_arg_names' =&gt; '3862724323261467',
                          'p_t03' =&gt; '',
                          'p_md5_checksum' =&gt; '',
                          'p_page_checksum' =&gt; 'BD638A6561F7DB9D1B514334733CB9CD'
                          ); //login form field names and values
  
          $login = getUrl($loginUrl, 'post', $loginFields); //login to the site
  
          $remotePage = getUrl($remotePageUrl); //get the remote page
  
          $result = $remotePage -&gt; 
  
          print_r($remotePage);
  
          function getUrl($url, $method='', $vars='') {
              $ch = curl_init();
              if ($method == 'post') {
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
              }
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
              curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
              curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
              $buffer = curl_exec($ch);
              curl_close($ch);
              return $buffer;
          }
  </code></pre>