1

I receive a byte array from a third party source that is a PDF. I need to convert that so a file is downloaded to the user. I've implemented the code below and a download does occur but it's just a blank file.

If I echo the content of $fileToDownload, it is correct (starts with %PDF-, etc) and the data is there. Not sure what I'm doing wrong. Thank you in advance!

//GET CONTENT
$fileToDownload = $file->DownloadSignedDocumentResult;

//START DOWNLOAD
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileToDownload));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($fileToDownload));
ob_clean();
flush();
readfile($fileToDownload);
exit;

CONTENT OF echo $fileToDownload

(this file contains sensitive information so I'm only pasting a small snippet of the beginning of the file - hopefully that will be helpful):

%PDF-1.4 %���� 2 0 obj <> stream Dx��a/s�, ��v��"��Z�DƸ�1N�?ש�W� �d����A9���>�ˍ�u��Ҭ����F��;.��0ҷ�B;��s&ݩ%�]GP@Xb���I�䃶���8I�۪IJ����cR��!&�Q?ZG"��u7��Ζ�uY{^����[}���-�E�R.���� �r�u�����<���8b� ��a��9�Óㅅ�)1�IJ��/�� �zb����vD�VN�vD�|���ȓ�A�0��Ͻ�o*m���Z�gz�oH��\���V*;�\�Ӌ�=9��y��c������fT�O�����Kr"����Ԁ��������|��m'��% �!�@F ����|�A�j�)9�������g"���^m�Al�1���Bn�C9ύl�p�o{���]�k��x�5^y֡|щ���/�qقu���)�F��n^ɞQ��6�Pc\e�۲]�8f� '�C[\v��U=���f��uIc�e�!�4^�ϻHCy��I��Gv(%�.I>��T�d!�l��׊R}�����B6k��=�C#���W.<���c��7���U���BI���! ����G�{�꾼L�2�q �I���Q�;;�P*�z�n��d尌(D�����aύ��7�����g�e���2����f�$��q�*�~+�H;R��t�Ofz����~|(�Jl��� l4�q�:�����w6.Z���ql݋�Њ+��hO��Ɔ�}<���pEs?�4З� h�07�����.���? ��s�F.�=�Z�Rz�9O�Hp��2��FR��nwH��{Lk�A�l{�f�u��g�p �@;���~���G>_n��P���ߪTJӗ�W����$pYJ<�Es�[�3J4=��/i��T)�(�����U�r�Jn��I��}��g��a�t�Ufw+�|�k�pu��� ���#Oy�J��y�\cOu���0[Y�������l~W|�'3-�.�0�AW�*�|��z�l�e��t8іDZ��Mh4dvS���*j���nHn���*��8w��~_��%�S��j7�xM�l�������t�YD@�� � K�(�!�*��P��[�U��(�@����z1S�����c�.9&�4����v�j�[}��F�%}���XS����X6�㕣�X��lV ��<�M>��C(ڼo��t;���i *ʸ-ua��a$�:��ȶ���}�Mz�׻���&H4���&�'[�o�l�C�|�ēM�c��'m$ ��)S�)+��� ̤�f>T0��x�] �w?w%���+˦�����,������]F�����aڲ�:��b���3f�D@��Kg�kL��5V�Ei^c�G_I���H5)$�Ap,�����:���UӧR�J8~�����|�Ev�cUt=iJ)*��� �j��MOB\���m����c�~Q:~��1����Ev���z�H#�H��F+:G}掺 �f�d��f<��>�$�N�m�;��.s]�/S���?R�3�+���'�����Ң�1{6=��g�-��v���|>��x�V��m"�"��̔QC�.Al��:r?���|2�u������Ħё|?^���,u]48���֫���&T~2�x'5����᫏{�8ϋ��� ����WH���s��d2��1�n���?l�Ed�T���T'��Qm�"Y3ԾiZ�9����/G!$��Mj���)�m{m� ����R4_k7�c�a�����"m q~N2b�rí�%�῔��4�����0�+y���㳯̇O�!���.�F�\qZ0�fSZ� �Α�֩��"���ڐYNJ��{�{W���4ԝC�rg���/�-(C�<�O@��oS�,k�ǡ�*�PXz��o���%PfO0�@���l��t�oyw�a@(���A���!�OK��o��½q�8�DZ�Y�X���E�5���8ê��[5�Ƌ���M�)����t~��w��h�P���u?�� �w�kĵ��]"� �*gV�n�����B���4iw-�29�6}��Se����;v��m����F�[|ӥ�x���؋U��$>M���a��vij�܌ �� v����~]�ocf�<��L�䍞���J#(����nik�ث�R�u�"� N��1�}�y�T�S����r��+�b�(9{7�����x��ux�ݚ}�6� �C�=޻�����v�X�g���;9t�b_���l��am�ODQ��|���,�o禗�@�hkuݑ����t�d�ڐ��F�~*vh2��[���՞=$X� �pqH�_�7Ƒ�&�O�gǹ��I�]F}[%K�0�YN���Y�}����$���˱��i�~�Jk�*�c+{��Ђ���͉�v��u�9R����$�a�\;�N��쮴Tո���7�SMG|��Î�y�U�]]'Z�,MB��H�g@�&�rc��E0��i�����үmy���ONt��)(���)�I�D��;_� ŭ���N�Ks<(���!hE_�0"� ;U�{e2C�X�+��#|D���"�_#�]�Xu�6,Z��2���wNy�(0�z���4�;pn�<� zwpD����1Clh�f���S!Q��C6��1G��'�P����"(�:��F� |aY�h�wL����o�G�tq�Q��,Hr�%(�Z�� ��J���>ŘŢz� u�j��#kг�z�cl��{�{�xRuJn���6��v�H7v �ocr�r����� ��L��%?���-&[r �ƢyF蒦O����v��a�������է������Y�Z�@��n?�i�f>Fh��B��w����}(�u�zm�~}�����!���&�n���]�- ��h9������na�bi�wS��q n#8��Y~��b=�441_����'c�ʠ=�b��wTj2�N*MO�$�}8["ƫg�6}�E�3�,�KE������_�e����dj�O3�n�7u]�<�m���_���a��.���+��&W�'�8�_�e����b���GI| �\�92c�V�[�������*���Q�{(����a@Ğ�ك���Ѧ�j��Ut�S�� �۝�FM�F�0C�Lr���uS�h��vI+��zC�n�$&��,@l�H����(naKf��f��� endstream endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> endobj 9 0 obj <> endobj 10 0 obj /Creator <1408C602>/Keywords <>/ModDate <075D8656FF7B0721B23ACA5714F625E800F1D85A3259B0>/Producer <0E06D746811B1741A233CB4F1FE822FD7CB48D0F7613B7E5483D7D79CCF00F2321>/Title <0E0ED714A13B587FF622AC0E55A236F00D8289116D39F6CC2C2B7A5082C2052938040A6AC8D9>>> endobj 12 0 obj <> endobj 13 0 obj <> endobj 32 0 obj <> stream �_ Q�"[�-�c��ozĖn¹#�b\���ו�Sn�i����cv��WS��WXi �hc4ŋ�ҳ� �3��/S(&�a�+����9�J����=�u�^N|4�]F��eBa]a� /M��uL���N����WK��dg�Z�^8gH��zEtqhB o<�ӣd�6�������_�B|������ ���XӑH^�q�7��ަ(���}���h�AN�]���o�<�w���p=7��7*�[����������,�=?�{�-K|l֊��tiÞP��r鍪4[����p�b-��8բ� weZ1k��-{)i���=*l�K���~�<.�8_����h������+x> stream �/�ă��3쫩0j|����KQ�����9D~�0ʛdP�*sa�^�!���j��b|μ���V�?�#�F �G��j� D��[��lE�@W+ȗ���+4��>$ "��������?< � _.�7K�j4���<6�+���+ �Y�h�G�'��=��C�������v�>V� ����Q��i�d���=��|@����m�99#{�߾Q2�^GL��X�w6�T(�'y����������K���Μ7U�*��+�NI1�0��z�#�Wn���i��̫���"��IS�ԅ4���|�\T��)/]��%m��b��A�u�V�p.�pD,z�߯u�O��k�.r}�ʚ�n��U�^�����"��ƙXv���WTd�戽v� � �^[~6Lj�w%X��8��2���s��ڜؐ �vZ�7ga j�"O��v=���i8N^�r�����O>����M �������uC��0W��S�@^��������3���]:�t#Pj�ϳ��*�> stream r�V[tg�u�B�0��l���%�ቚ���XM� ��.�0�Y�y�����Rt.�z[����Ή˨tnWUOH�B��'�"�޻�}SI��!Ǿ�7�~OVK�Cv�ѫ�y�� �����l�#y��$h�y�CJ�kq�rϴ��$mCg+�+������'�Сv��2���ԁ�N�����?<�V�58�%�.���ᕎЁ�(�u�i!Չ��fe��|���eq�<^9��ՙѩ'y�jϦ͏?g��D�Sb���rli� q�<��\�kL[J����@�k��ܥ#��s�ͅ����p�!�ZO�I��{����A���уƒ��A�/t�al�"�����GW���>��+��c���-�V�i�6IW�-\R�֨���l�4��f�~��'�к FuA�ԁ3��� ���;h^\˴L-���+�E�G(?s�y�� |�q�͟L�#]J ��[p��|Z��q[��G��A ��;\�����3Y�c��vc�tk�K��a�t�rm(ˆ����F�ĝRנD�6�efQ��3t��Wn���y*j~�P�vkt��6"� �������� ��1�a׆vX����?=���mv�7,��������/J+��E� �� /�N����UyMk�������k2���/����A��e���"C,��RG����Ӑ��J U�����JH�]Q���� endstream endobj 35 0 obj <>

1
  • can you do an echo of $fileToDownload and paste here? Commented Aug 9, 2013 at 14:05

1 Answer 1

1

As I understand, $fileToDownload contains the binary data of the PDF, so you should just

echo $fileToDownload;

and also change:

header('Content-Length: '. strlen($fileToDownload));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.