Hello,

I've been trying to create a script that will take defined pages of a PDF and display them to a webpage using PHP.

I've looked through the PHP Manual and only came up with ways to create a PDF, but no read options.

--Detailed Scenario--
My school wanted me to make a smartphone application for them using a free service called PhoneGap (allows you to write a smartphone application using HTML, CSS, and JS), they also wanted it so after I graduate they will have no issue in using it (automation). The application must have course descriptions as well as other stuff, the course descriptions are stored in PDF on their website. What I came up with for a solution (and I don't know if it is possible) is to "read" the PDF into a temporary file, and display those contents.

I would like to use a temporary file because the school won't update the PDF more than once a month (if that) and I don't want to kill the user's data plan.

To be exact of what the PDF contains:
- Flow charts
- Formatted text (bold, underline, etc)
- Tables
- Background Gradient (Which needs to be obliterated)

(I know this is a help forum so I am not asking for full code, I just want to get help in terms of what I need to look at so I can get this done)

Any and all help is appreciated.

Dani AI

Generated

Short answer for : parsing and reflowing arbitrary PDFs is brittle (as said). Two reliable server-side patterns get the job done and keep mobile data low: extract only the needed pages into a new PDF, or render the needed pages to images and serve those cached files to the PhoneGap app. Extraction keeps vector/text quality and usually smaller files; rendering-to-images guarantees visual fidelity for flowcharts and complex layouts and lets image-processing be applied to remove unwanted backgrounds.

Example: extract pages (CLI or PHP library)

# pdftk (command line)
pdftk source.pdf cat 2 3 5 output extracted.pdf

# simple PHP serve (after extraction)
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="extracted.pdf"');
readfile('/path/to/extracted.pdf');

Or use a PHP importer like FPDI to build a new PDF from selected pages and output it directly.

Example: render a page to an image (Imagick PHP extension)

$im = new Imagick();
$im->setResolution(150,150);
$im->readImage('source.pdf[0]');   // page 1 = index 0
$im->setImageFormat('jpeg');
$im->writeImage('/cache/page1.jpg');

Rendering workflows are easy to cache and serve as small JPG/PNG files to the app; set the PDF read resolution to control quality/size.

Notes and cautions: automatic gradient removal can be lossy. On rendered images, ImageMagick techniques like -fuzz plus -transparent or level/contrast adjustments can remove near-uniform backgrounds, but this risks harming subtle text or thin chart lines. If possible, request a "print" or no-gradient source PDF from the school. Also check hosting limits: many shared hosts lack pdftk, Ghostscript, or Imagick; Composer packages (setasign/fpdi, pdfparser) or a small VPS/microservice can fill gaps.

Suggested workflow: scheduled fetch of the remote PDF -> check Last-Modified/ETag -> extract or render only changed pages -> store cached files + manifest -> app downloads manifest and only pulls new/changed images. This keeps bandwidth low and maintenance simple.

Recommended Answers

All 2 Replies

There isn't an easy and obvious solution for this. You can generate a PDF with PHP but pulling it apart is a different matter. The easiest solution would be to generate a file at the source that has exactly what you want and then all you have to do is to display it. This could be a second version of the existing file. Trying to take an existing PDF file and then rework the content to give you what you need is certainly the hard way and may not be do-able.

A second best alternative, if you can find something to do it, would be a PDF reader which will take a parm for the pages that you want to display. I'm not aware of one, but you might find something if you do a search.

The least likely to succeed would be trying to read the content and manipulate it. The programs that can do this with some success are desktop applications. They often aren't 100% with graphics. I am not aware of any that have an API that lets you get detailed access to the content.

That's what I was afraid of...


Thank you for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.