PDF_add_thumbnail (PHP 4 >= 4.0.5, PECL pdflib >= 1.0.0) PDF_add_thumbnail — Add thumbnail for current page. A follow up to creating thumbnail previews of PDF documents quickly and easy with this basic walk-through from setup to code samples.
- Is there any way to create a thumbnail image (preview) of the first page of pdf file using jQuery or any other JavaScript plugins/libraries? I am using Apache server.
- Most PHP PDF libraries that I have found center. Create a thumbnail preview of documents (PDF, DOC. Convert a PDF document to a preview image in PHP without.
Create thumbnail previews of PDF documents quickly and easy with this basic walk-through from setup to code samples.
Generate Thumbnail Images from PDF Documents. This article presents VB. NET code to create thumbnail images from a directory of Adobe Acrobat PDF documents using the . NET Framework. Introduction. This article presents VB.
NET code to create thumbnail images from a directory of Adobe Acrobat PDF documents. Often when looking for documents it is much easier to find what you want visually, for example seeing the cover of a document. The application was written for a website that I was developing that needed to display links to PDF documents.
Instead of just showing a little PDF icon next to each document we wanted to display the front page of the actual document. As shown below, this gives the listings better aesthetics and also enables the users to find documents quicker if they recognise it.
VSNote: please ignore the strange text, lorem ipsum is simply dummy text for this example. Hopefully people will agree that having the actual front cover displayed next to the hyperlink works better than the generic PDF icon.
Background. The web site was a Content Management System (CMS) so new PDF documents were uploaded to the site by the users. We then had this application scheduled as a batch service to run every 5 minutes and check for new files. In the backend system the documents have metadata stored in a SQL Server 2. We would then write a flag to say the thumbnail had been created and when we generated the HTML content for the page request in ASP/ASP.
NET we would return the appropriate IMG tag and source as appropriate. Using the Acrobat SDK also meant we could programmically read the PDF metadata and retrieve the number of pages in the document, which could then be displayed as well.
Although the end users could have entered that information it meant less work for them and a better overall impression of the web site. Another advantage was that many users relied on the number of pages to determine how large the document was rather than the more technical Kb/Mb value. Approach. To generate the thumbnail image for each document I used the Adobe Acrobat 5.
SDK and the Microsoft . NET 1. 1 Framework. Note: do not confuse the thumbnails that are part of a PDF document with the . The Acrobat SDK combined with the full version of Adobe Acrobat (sadly the free reader does not expose the COM interfaces) exposes a COM library of objects that can be used to manipulate and access PDF information. So using these COM objects via COM Interop, we can load the PDF document, get the first page and render that page to the clipboard.
Then using the . NET Framework we can copy this to a bitmap, scale and combine that image and then save the result as a . At first I just saved the scaled down image, but then decided to “fancy” up the thumbnail with a drop- shadow and folded corner. To achieve this effect I created a transparent . Macromedia Fireworks MX where the main body of the page template was transparent. By making the bottom- left pixel transparent too we can easily set the transparent colour for a bitmap in . NET. I keep the top- right of the image white where the corner folds over, that means I can just combine the images by drawing the transparent template directly over the PDF image to achieve the final look. Pre- requisites. The full version of Adobe Acrobat (the free reader does not expose the COM interfaces) which exposes a COM library of objects to manipulate and access PDF information.
The Adobe Acrobat 5. SDK which is a free download from the Adobe Solutions Network website (note: the site requires registration).
The latest SDK for Acrobat 6. SDK version. To quickly see if you have the full version of Adobe Acrobat installed, use regedit. HKEY_CLASSES_ROOT for entry entry called Acro.
Exch. PDDoc. You'll also need the . NET 1. 1 Framework and some PDF files to test the solution. The code was written in VB. NET using the . NET 1. Framework and Visual Studio. NET 2. 00. 3 on Windows XP, but there is no reason it wouldn't work on Windows NT/2.
NET 1. 0. Using the code. The code is quite simple with a try/catch over the main body. It is purposely in one large block so it's easy to see what it happening and to step through and examine with the debugger.
Initially we create an instance of Acro. Exch. PDDoc using late- binding. The referenced Adobe Acrobat 5. Type Library (Acrobat. C: \Program Files\Adobe\Acrobat 5. SDK\Inter. App. Communication. Support\Headers) does not expose a COM class you can create using early- binding.
By referencing the type library we can get the Intellisense and strong- typing of the other Acrobat objects. Pass the filename of the PDF documents to be opened to the PDDoc object, which can then be accessed to get metadata on the document; Get. Num. Pages() and Get. Info() for custom document properties. Doc = Create. Object("Acro. Exch. PDDoc"). ret = pdf. Doc. Open(input. File).
If ret = False. Then. Throw. New File. Not. Found. Exception. End. Ifpage. Count = pdf. Doc. Get. Num. Pages()Set a reference to the first page of the document as pdf.
Page, which is of type Acrobat. CAcro. PDPage. From this we can get a rectangle object of the actual page dimensions. One strange point to notice here is that the Adobe Acrobat SDK documentation seems incorrect, as the PDFRect that is returned from the Get. Size() method has IDispatch properties x, y but the PDFRect we need to supply to Copy.
To. Clipboard must have left, right, top, bottom. Finally we render the PDF page to the clipboard at full size. We could have Acrobat scale the image down for us by a percentage, but we can get better visual results using the . NET scaling algorithms of the Bitmap class. It would have been more efficient to render directly to an off- screen bitmap, and also not have overwritten what ever was previously on the clipboard, but I found the clipboard method the most stable way to get a rendered bitmap of the page using Acrobat.
Although it looks like the pdf. Page object has a Draw.
Ex method that can take an H< CODE> DC I couldn't get the method to work in a consistently successful way. Calling Draw. Ex in the paint event of a Windows Forms application did work but it still wouldn't write to an off- screen bitmap directly. Therefore the clipboard method is used and if the process runs on a batch server it won't cause too much worry. Note: the Draw method is deprecated, as it only works on Win. Wnd was unique to Windows and not to each process as on NT.
Page = pdf. Doc. Acquire. Page(0). pdf. Rect. Temp = pdf. Page. Get. Size. pdf. Rect = Create. Object("Acro. Exch. Rect"). pdf. Rect. Left = 0. pdf. Rect.
Rect. Temp. x. pdf. Rect. Top = 0. pdf. Rect. bottom = pdf. Rect. Temp. y. Call pdf. Page. Copy. To. Clipboard(pdf. Rect, 0, 0, 1. 00). Dim clipboard. Data As IData.
Object = Clipboard. Get. Data. Object()Grab the rendered page bitmap from the clipboard and based on the pdf. Rect. Temp object determine if it's a portait or landscape document.
Set the correct file to load as the template, and if it is landscape, switch the width and height. Dim pdf. Bitmap As Bitmap = clipboard. Data. Get. Data(Data. Formats. Bitmap). Dim thumbnail. Width As. Integer = 3. 8Dim thumbnail.
Height As. Integer = 5. Dim template. File As. String. If (pdf. Rect.
Temp. x < pdf. Rect. Temp. y) Then. File = template. Portrait. File. template. File = template.
Landscape. File. thumbnail. Width = thumbnail.
Width Xor thumbnail. Height. thumbnail. Height = thumbnail. Width Xor thumbnail. Height. thumbnail. Width = thumbnail.
Width Xor thumbnail. Height. End. If. Load the template file as as Bitmap and as an Image. We use both because the Bitmap class supports Make. Transparent and the image can easily be passed to the Graphics. Draw. Image() method. It is slightly inefficent but speed isn't the primarly objective for this application. Render the pdf. Image using the Get.
Thumbnail. Image() method of the . NET Framework Bitmap class, this provides a very smooth scaled version of the image. Next create a blank bitmap with room for the template border. Set the template. Bitmap to use the bottom- left pixel of the image as the transparency colour using calling Make.
Transparent(). See an article on Chris Sells website for more on transparencies in . NET. Using the new blank bitmap, draw the rendered pdf page image to it and then the template with transparency directly over the top. Because it is transparent the main area of the page template will still appear through.
Finally, save the composited image back as a . Dim template. Bitmap As Bitmap = New Bitmap(template. File). Dim template.
Image As Image = Image. From. File(template. File). Dim pdf. Image As Image = pdf. Bitmap. Get. Thumbnail. Image(thumbnail. Width, _.
Height, _. Nothing, Nothing). Dim thumbnail. Bitmap As Bitmap = New Bitmap(thumbnail. Width + 7, _. thumbnail. Height + 7, _. Imaging. Pixel. Format. Format. Argb). template. Bitmap. Make. Transparent().
Dim thumbnail. Graphics As Graphics = Graphics. From. Image(thumbnail. Bitmap). thumbnail.
Graphics. Draw. Image(pdf. Image, 2, 2, thumbnail. Width, thumbnail. Height). thumbnail. Graphics. Draw. Image(template.
Image, 0, 0). thumbnail. Bitmap. Save(output. File, Imaging. Image. Format. Png)Write some feedback to the console as we work through each of the files. Then actively release the reference code to the COM objects as Acrobat it isn't the best suited application to opening and closing multiple PDF documents without falling over. Luckily the code doesn't cause Acrobat to display any UI that might cause the process to hang waiting for user interaction. Console. Write. Line("Generated thumbnail..
File). thumbnail. Graphics. Dispose(). Marshal. Release.
Com. Object(pdf. Page). Marshal. Release.
Com. Object(pdf. Rect). Marshal. Release.
Com. Object(pdf. Doc)Visual Studio. NET Solution. The project you can download has all the VB. NET code and the COM Interop DLL that was generated. Even though the application is actually a console application we still need System. Windows. Form as the clipboard dataformats are from there. Use the app. config to set the input and output paths for the .
How do I convert a PDF document to a preview image in PHP? For those who don't have Image. Magick for whatever reason, GD functions will also work, in conjunction with Ghost. Script. Run the ghostscript command with exec() to convert a PDF to JPG, and manipulate the resulting file with imagecreatefromjpeg(). Run the ghostscript command: exec('gs - d. SAFER - d. BATCH - s. DEVICE=jpeg - d. Text.
Alpha. Bits=4 - d. Graphics. Alpha. Bits=4 - r. Output. File=whatever. To manipulate, create a new placeholder image, $newimage = imagecreatetruecolor(..), and bring in the current image.