简体   繁体   中英

Render pdf file in a browser

I am trying to display pdf in broswer window and getting some text as a response.Please tell me where I am doing the mistake.Here's my code:

$filepath = 'ssh2.sftp://<ftpuser>:<ftp password>@<ftphost>/scratch/user1/opt/portal/7.5/custom/apps/cus_transfer_invoice/BA10000892/B1-13370/Invoice_BA10000892_B1-13370_189.pdf';

header('Content-type: application/pdf');

header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: bytes');
readfile($filepath);

I am getting the output as something like this :

JVBERi0xLjQKJeLjz9MKMiAwIG9iago8PC9MZW5ndGggNTIvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwr5HIK4TI2UzA3M9Azs1AISeFyDeEK5CpUMFQwAEIImZyroB+RZqjgkq8QyAUACHQKjQplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwvVHlwZS9QYWdlL01lZGlhQm94WzAgMCA2MTIgNzk2LjY4XS9SZXNvdXJjZXM8PC9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXS9YT2JqZWN0PDwvWGYxIDEgMCBSPj4+Pi9Db250ZW50cyAyIDAgUi9QYXJlbnQgMyAwIFI+PgplbmRvYmoKNiAwIG9iago8PC9MZW5ndGggNTIvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwr5HIK4TI2UzA3M9Azs1AISeFyDeEK5CpUMFQwAEIImZyroB+RZqTgkq8QyAUACHsKjgplbmRzdHJlYW0KZW5kb2JqCjcgMCBvYmoKPDwvVHlwZS9QYWdlL01lZGlhQm94WzAgMCA2MTIgNzk2LjY4XS9SZXNvdXJjZXM8PC9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXS9YT2JqZWN0PDwvWGYyIDUgMCBSPj4+Pi9Db250ZW50cyA2IDAgUi9QYXJlbnQgMyAwIFI+PgplbmRvYmoKOSAwIG9iago8PC9MZW5ndGggNDkvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwr5HIK4TI2UzA3NVMISeFyDeEK5CpUMFQwAEIImZyroB+RZqzgkq8QyAUA60cJ+AplbmRzdHJlYW0KZW5kb2JqCjEwIDAgb2JqCjw8L1R5cGUvUGFnZS9NZWRpYUJveFswIDAg"

Remove the Content-Length header. That's almost certainly your problem. See eg Sending correct file size with PHP download script If it works without that header, you can either try to work out exactly why the header is wrong, or just send no content-length (which basically will work OK)

See What are the consequences of not including a content-length header in a server response? for more on what happens if you just go without a Content-Length header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM