Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

How to create PDF preview images in PHP


h3nryk

Recommended Posts

Hi,

The Problem:

I want to show a preview or the whole PDF content in image form.

 

I've found this in web: (link)

 

Requirements:

The following applications need to be installed on the server you are running your webserver.

- ImageMagick

- GhostScript

 

The Code

To create the images of the PDF we call the external ImageMagick application from the command line. In PHP this is done like so:

1. <?php
  2. exec(‘command line to run’);
  3. ?>

So to run ImageMagick from the command line we do:

 

 1. <?php
  2. exec(‘/path/to/imagemagick’);
  3. ?>

 

The paramters we then use to call ImageMagick depend on what we want to do, and consulting the ImageMagick help pages are the best way to learn exactly what is happening.

 

Below are some common examples:

 

Create GIF Thumbnail of First PDF Page

 

   1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4.
  5. exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.gif\"");
  6. ?>

Create JPEG Thumbnails of ALL Pages Within PDF

 

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4.
  5. exec("convert \"{$strPDF}\" -colorspace RGB -geometry 200 \"output.jpg\"");
  6. ?>

 

Create Large PNG 1024px Image of First PDF Page

 

   1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4.
  5. exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 1024 \"output.png\"");
  6. ?>

 

Create Large PNG 1024px Images of ALL Pages Within PDF

 

   1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4.
  5. exec("convert \"{$strPDF}\" -colorspace RGB -geometry 1024 \"output.png\"");
  6. ?>

 

As you can see the code is very simple as we are just calling an external command line application to do the hard work – the only thing we have to do is provide the parameters to tell ImageMagick what to do.

 

I just don't understand few things..

Questions:

 

How to instal: ImageMagick and GhostScript using CPanel or FTP ? Can I do do this myself or need to ask server support ?

Where to install, how ???

 

Instead of product image I want to show PDF file preview - one or more pages of ebook, how to do it ?How to use the code ?

 1. <?php
  2. exec(‘/path/to/imagemagick’);
  3. ?>

  1. <?php
  2. //the path to the PDF file
  3. $strPDF = "my_pdf.pdf";
  4.
  5. exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.jpg\"");
  6. ?>

Edited by h3nryk
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...