2 Replies - 99003 Views - Last Post: 16 July 2012 - 07:15 PM Rate Topic: -----

#1 huskyfritz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 04-July 09

How to read/display pdf in a browser using PHP

Posted 16 July 2012 - 05:59 PM

Hello guys! All I want to do now is to READ a pdf file using php. Is there any easy way to do that? I've done searching here and tried some of the codes but still no luck. Other people here recommends PDfLib but its not free.

I've also done this simple way but failed:
<?php
   $contents=file_get_contents("manual.docx");
   echo $contents;
?>



Do you have any plugins to do that? Any sample script?

I will appreciate any reply. :)

Thanks,
Joery

Is This A Good Question/Topic? 0
  • +

Replies To: How to read/display pdf in a browser using PHP

#2 creativecoding   User is offline

  • Hash != Encryption
  • member icon


Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: How to read/display pdf in a browser using PHP

Posted 16 July 2012 - 06:58 PM

I'm actually looking into a project that uses PDFs right now. I haven't tried it, but this library looks promising: http://www.fpdf.org/
Was This Post Helpful? 1
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to read/display pdf in a browser using PHP

Posted 16 July 2012 - 07:15 PM

creativecoding said:

I haven't tried it, but this library looks promising: http://www.fpdf.org/

If I'm not mistaken, FPDF only supports writing PDF files, not reading them.

I haven't tried doing this myself, but the PDFlib wrapper is the only one I know of that can actually read PDFs. There are some free command line tools available for most OSs though, so if you can install that on the server you may be able to use the exec functions to extract the PDF data from the PDF file into something more... useful.

huskyfritz said:

I've also done this simple way but failed:
<?php
   $contents=file_get_contents("manual.docx");
   echo $contents;
?>


First of all, that is a Word Document file, not a PDF file. Second, all you are doing there is dumping the raw data from that file into the request body, which the browser will read as HTML, which will in all likelyhood result in a complete mess.

If you want to send a PDF file to the user's browser for it to be display by the browser's own PDF reader (if any), you would have to let the browser know you are sending a PDF file, and then send the data. Something more like:
<?php
// The location of the PDF file on the server.
$filename = "/path/to/the/file.pdf";

// Let the browser know that a PDF file is coming.
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));

// Send the file to the browser.
readfile($filename);
exit;


Note that PHP is not actually reading the PDF file. It has no idea what kind of file you are sending. It only passes the PDF file to the browser to be read there. If the browser doesn't know how to read it either, then it will probably just download it rather than show it.
Was This Post Helpful? 4
  • +
  • -

Page 1 of 1