Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get content of PDF file in PHP

Tags:

php

parsing

pdf

I have a FlipBook jquery page and too many ebooks(pdf format) to display on it. I need to keep these PDF's hidden so that I would like to get its content with PHP and display it with my FlipBook jquery page. (instead of giving whole pdf I would like to give it as parts).

Is there any way i can get whole content of PDF file with PHP? I need to seperate them according to their pages.

like image 370
Berk Kaya Avatar asked Apr 23 '16 10:04

Berk Kaya


People also ask

How to extract text from PDF in PHP?

How Can PHP Extract Text from PDF? You can retrieve individual page contents by using the Pages array property which is available, like the Text property, once the PDF file contents has been loaded. The Pages property is an associative array whose keys are page numbers, and values, page contents.

How to embed a PDF on a web page in PHP?

To embed the PDF on a web page, we use the iframe element. Here is the PDF file displayed on the webpage that is fetched from the MySQL database table using the PHP code: In the above method, we have embedded a PDF in an iframe. This is basically used when we have to show a pdf in addition to the web contents.

How to display PDF as the content of a webpage?

This is basically used when we have to show a pdf in addition to the web contents. But if you want to display PDF as the whole content of a webpage, then it's better to use the header () function. In this below example, we have added header () function and set the file 'Content-type', 'Content-Disposition', and 'Content-Transfer-Encoding'.

How to generate a PDF file in PHP using FPDF?

You need to download the FPDF class from the FPDF website and include it in your PHP script. Instantiate and use the FPDF class according to your need as shown in the following examples. Example 1: The following example generates a PDF file with the given text in the code.


1 Answers

You can use PDF Parser (PHP PDF Library) to extract each and everything from PDF's.

PDF Parser Library Link: https://github.com/smalot/pdfparser

Online Demo Link: https://github.com/smalot/pdfparser/blob/master/doc/Usage.md

Documentation Link: https://github.com/smalot/pdfparser/tree/master/doc

Sample Code:

<?php
 
// Include Composer autoloader if not already done.
include 'vendor/autoload.php';
 
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf    = $parser->parseFile('document.pdf');
 
$text = $pdf->getText();
echo $text;
 
?>

Regarding another part of your Question:

How To Convert Your PDF Pages Into Images:

You need ImageMagick and GhostScript

<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

The [0] means page 1.

like image 85
Umair Shah Yousafzai Avatar answered Oct 16 '22 20:10

Umair Shah Yousafzai