如何使用PHP将PDF流解码为可下载文件?

时间:2018-07-25 17:08:09

标签: php pdf decode

我正在运行一个PHP脚本,该脚本从网站提取PDF文件。一切正常,但是我的问题是,一旦获得PDF文件,我便不知道如何将其转换为可下载的文件。这是我从脚本中获得的输出:

%PDF-1.6
5 0 obj
<<
/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
>>
endobj
6 0 obj
<< /Length 4407 /Filter /FlateDecode >>
stream
x��[ms�H��������[u�*[V�Z-��0&   ;��̮omɠ��b$���}NK�q���7�����s�>oRlf������f���WW�ׯ��ƙ�Z�e���������7��+�m6���U�:̣K�l�B�as�������-.�ܱ|���EA�N}��!�a���`�Vs�C,W2O���1���,��Gڕ~�$�͓pOXA�3�������
&��t|�w����׉��q����tF�:�4�.�嚋�n�(��<
7�6Is��`j+���b}w����|��|�f�mo>��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>g��ߍ�³���"��d�S�eǒ�ep�<��?���������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>�ˈ���X��<�K0)���   G8��x�'�3�~�6����qe�-[���z�+�P�T_~�Vav��1�<h��R*�B�^�]��a��VG�a~<���xp�^ږ氁)�oW���W+�|d�p    ��F��}��Kָ9qWJn�r[L�n)@�����r�J�5�~w��GVl�*w,Q��iC
(v$.&mQ��gy��"��+���(XS���¨���D����=�.BI��U�6��JN�h�S��I�����e�H%��jۗ��R(��/�M d-�e�I���1g��0}��<J��%���!�hpq\>�\�K�B@
�݆��?��42=u⎳�fE�ěe��-Y�?F�S�E�݄3sep3�w��<'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�&�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��&���������2�cd=i��.��˶�"�U�8���d�oJ�h(ϣ
�!�am�E���*Ā0[%O��z���>bY��a�   ��m��>�K��h���Ys��7�:Y�1by���ux��$)ҎA�d��,�f�L�u"�G���͛^�������5����4���!��f�u

��������F�
�u������o9��ᚍUG���(�%V�T}Z��=>)�'�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����
(<�}��� D�m���Z����۔��Ή�M,���Lе!��=� 1��[$v}^x���K畅j\��Lj�(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�<N��&!����ߦf�dU��i��s���.��:`����],�mޯ"j�3]>��kܺ1�hR���ٯ��'�x�Y ��->�ڻ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�<��~��n���E�H�Ѽ,�o�M����2J�"]��}��˳(p�B�6�h�/��q=�jʖ��*Pg�����ô�.&��/&�;o��\8�[=1�:�(q6��*Z>�zk��\����t!���l9�[�?�U�g�Y��`Q^�E�(}�6�g�Cy���7� �2��7\*��5�'��u�D|��+�^H���E��#�\#�=l������e���}�|��KF̥<a�,v�ª��=Ƌ��vި�xU�W�xe�4����>�����LJ$�pH���Y+w8!^;ZZ��P�SF/����M��z-{���\�����>G��_�ʲ�cs�V�ֈ�,�������8'�!�>LQڟ��.�_X�s3�F�1))�@Y�Ԙ*�C'��-H��"�x9�-ys�%}t)Y/��QB����>^�0�*F�+l��U�e:3׸d����,�R����p�]!�_��>G[N�RٯZ���ճ�����:����:���J#��u��,���UF4����_��Hjw���9�w���s���$p�H2�
�GI�>�"��i��3�&�q����(�T�hۮΫ���mq5���>ʟ�����n���Rc\���
�83=����Ծ6�Xz�u��o?��4�DWh�K�
_::�a�|���:��*���De�O#\BD���9�x�!�nU�m^Aeo�_P�:��
�I�����>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&�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�\��=����>����x�2
�!k���(�$SM��G
��ö��6�}F�G;��t��$F=vxq���+z��_㶸W�&³�;��V�n�g�Z����4�u.<��x�~V��+KR���&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����&kS�&>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
<<
/Type /Catalog
/Pages 3 0 R
>>
endobj
2 0 obj
<<
/Type /Info
/Producer (Oracle BI Publisher 11.1.1.9.0)
>>
endobj
3 0 obj
<<
/Type /Pages
/Kids [
5 0 R
]
/Count 1
>>
endobj
4 0 obj
<<
/ProcSet [ /PDF /Text ]
/Font << 
/F1 7 0 R
/F2 8 0 R
/F3 9 0 R
>>
>>
endobj
7 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
endobj
8 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica-Bold
/Encoding /WinAnsiEncoding
>>
endobj
9 0 obj
<<
/Type /Font
/Subtype /Type1
/BaseFont /Helvetica-Oblique
/Encoding /WinAnsiEncoding
>>
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
<<
/Size 12
/Root 1 0 R
/Info 2 0 R
/ID [<b6e546c805d4be27e2c7679a536a9863><b6e546c805d4be27e2c7679a536a9863>]
>>
startxref
5389
%%EOF

我尝试搜索PHP脚本示例,其中解码了流字符串,但似乎找不到很多。有谁知道如何做到这一点?

****更新****

这是我用来获取PDF文件的代码:

$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'=>'username', 
                        'p_t02'=>'password',
                        'p_flow_id' => '77055',
                        'p_flow_step_id' => '101',
                        'p_instance' => '104062064171792',
                        'p_page_submission_id' => '4717306236799',
                        'p_request' => '',
                        'p_arg_names' => '3862724323261467',
                        'p_t03' => '',
                        'p_md5_checksum' => '',
                        'p_page_checksum' => '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 -> 

        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;
        }

1 个答案:

答案 0 :(得分:1)

尝试添加此代码:

$buffer = curl_exec($ch);
   header('Cache-Control: public'); 
   header('Content-type: application/pdf');
   header('Content-Disposition: attachment; filename="new.pdf"');
   header('Content-Length: '.strlen($buffer));
   echo $buffer;