read pdf files with php directly in the browser

Asked

Viewed 5,235 times

-3

Colleagues. Is it possible to open a PDF file directly inside the browser? I tried file_get_contents() and fopen(), but it opened decoded. So I tried that way, but it opened in the entire browser:

$arquivo='0eb178d150b82f5536588fbfea5fa7bd.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="0eb178d150b82f5536588fbfea5fa7bd.pdf"');
 @readfile($arquivo);
  • 1

    The first question is, did you send the correct mime-type on the headers? As you already have a site time, you should know that if you post your code, it is easier to locate the possible problems.

  • Hello Bacco, you’re right, I forgot to put the code. I edited my question.

  • I just don’t think it’s right for them to give us a negative sign before we manifest, because this is a matter of urgency, and by closing the topic, it will slow down those who really need help. The right thing to do is to wait for the person who posted the doubt to answer the question before giving negative points.

  • 1

    Try without the @ to see if there is an error. And don’t worry about the negative, it doesn’t close the question. By the way, if you left without the code, it would be normal to close as "not clear", but this is different from negative.

  • Right Bacco. The problem is that it opens in the entire browser and only need to open inside a div...

  • Well, the last Dit has already changed the issue completely. If you want to download, exchange the application/pdf for application/octet-stream, if you want to open it in a div, then only with an IFRAME or new window. In IFRAME I don’t know if I can, to tell you the truth.

  • I changed it, but he asks me to change it...

  • I get it......

Show 3 more comments

1 answer

1

<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);
?>

Source: https://stackoverflow.com/questions/4679756/

Code working http://triviapw.hiperportal.blog.br/wesley/pdf_in_browser/pdf.php

  • Sorry, only after answering, I saw that you need to display inside the div. In this case the iframe will serve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.