Extension:PdfHandler

The PdfHandler extension shows uploaded pdf files in a multipage preview layout.

With the Proofread Page extension, pdfs can be displayed side-by-side with text. This allows users to transcribe books and other documents.

Pre-requisites
This extension requires the following packages to be installed first:

Type into a shell  to see if you have the above installed first.

Installation
The required software above must be installed first.

Usage

 * A user can display PDF files as an image, showing a single page at a time, like so: myPdfFile.pdf . The page and size parameters are optional; the default page is page #1. Instead of a size-parameter, you can also use the thumb-parameter, with or without captions: myPdfFile.pdf.
 * Because PdfHandler extends ImageHandler, you can use all the arguments that you would for an Image -- for example: thumb, right/left, caption, border, link, etc.
 * If you would like to present a 2-page pdf, for example, do the following: myPdfFile.pdf myPdfFile.pdf


 * The main usage of the PdfHandler extension is without user interaction. If you upload a new pdf file, the metadata will be stored in the database, and then this file can be shown in a multipage preview layout like the djvu handler does. Without this extension, pdfs will not display properly when uploaded.
 * Additionally, this extension allows Extension:ProofreadPage to handle pdfs in side-by-side view for transcribing/proofreading, as is done on Wikisource

Configuration
You can (or, depending on the operating system of the server, will have to) set some variables in the "LocalSettings.php" file:
 * (default = "gs")
 * path to your ghostscript implementation


 * (default = "convert")
 * path to your imagemagick convert


 * (default = "pdfinfo")
 * path to your pdfinfo


 * (default = "pdftotext")
 * path to your pdftotext


 * (default = "jpg")
 * extension of the files to be rendered


 * (default = "150" )
 * resolution in dpi
 * The extension extracts a bitmap image for each page of the PDF, using this resolution (dpi = dots per inch). For example, a PDF page with the European size A4 is 210 mm wide, corresponding to 595 points (at 72 dpi). This yields an image 1240 pixels wide (at 150 dpi). If instead this parameter is set to 300 dpi, the width will be 2480 pixels.


 * (default = "95" / since MW 1.24+ )
 * Quality level, which the post processor should use.


 * (default = "false")
 * Puts creating pages' thumbnails into a job queue, so they do not have to be created while browsing a file page, but during normal wikibrowsing. Be advised that setting this to "true" may significantly increase CPU load of your webserver on a high-traffic website. Job queue was designed to perform quick tasks on page views, and creating thumbnails can be included into that queue, according to this definition. Nevertheless, as quick as it is, it also requires certain CPU load to convert PDF pages to another format. The solution to that would be either setting $wgJobRunRate to rather small value (ie. 0.05) or disabling job queue on the high-traffic wiki and setting up another server to do just job queue (like Wikimedia did).


 * Variables below are not specific to this extension
 * Enable PDF uploads, if you haven't already: $wgFileExtensions[] = 'pdf';
 * Enable ImageMagick, if you haven't already: $wgUseImageMagick = true;
 * $wgMaxShellMemory - memory limit for gs, convert and pdfinfo. The default value might be too low.

Ubuntu
This is identical to the default settings for this extension.

macOS
To install dependencies via Homebrew:



Debugging

 * If PDFs don't display after upload, make sure that MediaWiki can execute the pdfinfo command and that  is set properly. Also check your error log, and make sure that your host hasn't disabled running external commands.
 * If PDFs don't show properly after installation you may need to use the maintenance scripts  and
 * If PDFs are rendered randomly check if the "C.UTF-8" locale is available on your server by running  and make sure that   is set to this locale.

Special solutions for Windows Server running MW 1.27
If you are running this extension on a windows machine with PHP 7, you may need to enclose the commands in the PdfHandler.image.php file.

If you get error messages like

or   and if your thumbnails neither are creating nor displaying meta data such as height, width etc. this may fix your problem!

This message appears, when PDFHandler retrieve metadata from pdf file via running pdfinfo.exe and pdftotext.exe. and The function is called. This happens due a bug in PHP.

Tested with MW 1.27 PHP 7.0.9 x64 on an Windows Server 2012 R2 and an Windows Server 2008 R2.

Thanks to the user Urmatskih

Special solutions for Windows Server running MW 1.31.1
If you are running this extension on a windows machine with PHP 7, please see Tommeyheyser's workaround here

If you are having pdfoinfo and/or pdftotext hanging issue that prevents a big PDF upload, please check the modified extension by SeongMoon