banner



How To Use Ghostscript In Windows

The Context

Have you lot ever run into a situation where you've got a PDF file and desire to easily generate an image from ane or more of its pages? Or excerpt its text content? Or merge multiple PDFs? Or convert an image to various raster formats? Yes, sure, there are many programs that you tin install to do any of the above.

The problem is: what happens when you have many different PDFs each of which have around 100 or more pages? What happens when the pet shop is closed and yous tin't buy a gazillion pet monkeys and train them to do PDF to image conversion? What happens when Minions-R-Us has a going out of business sale and you weren't able to observe parking in time earlier all the minions got sold out?

If y'all can personally relate to any of these three questions (and hopefully just the beginning question, because training monkeys to exercise your behest is probably illegal and only so that you know, minions don't actually exist… deplorable…), so the solution to your issues tin can be found in using Ghostscript.

What is Ghostscript?

Ghostscript is software that makes use of an interpreter for a PDF's page description language as well as the PostScript programming language created past Adobe Systems. Yous can use PostScript in your programme to create vector graphics. Vector graphics are geometrical primitives such equally points, lines, curves, shapes or polygons that are mathematically generated to represent images in figurer graphics. Since vector graphics are mathematically generated, they tin can be infinitely enlarged without losing any quality.

Compare vector graphics to rasterised graphics such equally Bitmaps, Jpegs or PNG images, where the image is made out of millions of pixels – tiny squares of single colours that are organised together in such a mode that it displays a full prototype. If you enlarge the epitome, yous are substantially just enlarging each i of the tiny squares or pixels that the epitome is made of. Quality volition get lost every bit a consequence, because you will beginning to see the individual pixels and the image will not appear as smooth anymore.

Vector graphic vs raster image at 10x magnification

Vector graphic vs raster image at 10x magnification

The content of a PDF page is generated using vector graphics and that is why you tin zoom into a PDF page without information technology always losing quality like with rasterised graphics. To view PDFs, y'all demand a special program in order to interpret the PDF'southward page clarification linguistic communication and draw the vector graphics. These programs generally only allow you to read the PDF. If you desire to generate a rasterised prototype of one or more of the PDF's pages, you would mostly need a dissimilar program that provides that functionality. Just using those programs crave a pregnant amount of manual labor, especially if yous take many large PDFs that you want to process.

This is exactly where Ghostscript makes its dramatic entrance.

Ghostscript is written entirely in the C programming language and has been ported to run on a broad variety of operating systems including Microsoft Windows, Apple Mac OS and the many different types of Unix-based operating systems. There are various graphical user interfaces available that brand employ of Ghostscript to allow you to view a PostScript file or PDF on screen. Some even allow you lot to generate images from PDF pages, simply as mentioned earlier, it can exist a very time consuming chore, particularly if you lot need to convert hundreds of PDF pages. In this web log post we're going to await at using Ghostscript in the console or final in order to batch process a PDF.

Installing Ghostscript

The most contempo release of Ghostscript can be downloaded directly from the Ghostscript website. If you're running Microsoft Windows, you tin can download and install the standalone *.exe file. If you lot're running Apple Mac Bone, y'all can download the standalone build which is accessible through the Terminal. To build and install Ghostscript from its source, download the cross-platform source annal. In one case you've downloaded the source  nil archive, you can extract it to a folder on your figurer. If you're running Apple Mac Os, open the Terminal app and browse to the extracted binder and run the configure script e.g.

          cd /path/to/ghostscript-#.#     ./configure

This volition run several scripts in order to generate a 'make' file. Once the scripts are washed building the 'make' file, you can run the 'make' file installer by simply entering the post-obit command:

          brand install

The arrangement will then run all the necessary scripts to build Ghostscript.

What you can expect to see while Ghostscript builds and installs. All I see is blonde... brunette... red-head...

What you can await to meet while Ghostscript builds and installs. All I see is blonde… brunette… red-head…

Once it completes, you tin run into if it installed correctly by checking the currently installed version by inbound the following command:

          gs --version

If Ghostscript was installed successfully, the electric current version will exist displayed due east.g. 9.14

How to utilize Ghostscript

Following are some basic examples of how to use Ghostscript for various tasks. For a detailed guide on how to use Ghostscript, you tin view the official documentation online. These examples presume that you congenital Ghostscript from the source and are running Apple Mac Bone, but you should be able to easily apply the standalone builds also. You lot will simply need to motion the standalone builds into your working folder.

Convert PDF Pages to Images

Open up the Last and navigate to a binder on your calculator where you've got a PDF that you want to convert its pages to images. If y'all enter the following command, Ghostscript will convert each PDF page into a PNG image that fits into a pixel dimension of 768×1024:

          gs -dNOPAUSE -sDEVICE=png16m -sOutputFile=epitome%i.png -dPDFFitPage -g768x1024 -q instance.pdf -c quit

Following is an explanation of each of the flags:

  • -dNOPAUSE: do non interruption and await user input before standing on to the side by side page
  • -sDEVICE: fix the output file format, in this case a PNG with 16-million colours (other formats include e.g. pnggray, pngalpha, jpeg, jpeggray etc.)
  • -sOutputFile: the filename of the output prototype(s). Utilize %i or %d as a placeholder to automatically create images with the page number
  • -dPDFFitPage: scale the PDF page to fit into the specified dimensions. Aspect ratio is retained
  • -g: the dimensions in pixels of the generated paradigm
  • -q: placidity manner does not output progress to the Terminal window while the PDF is being candy
  • -c quit: after processing is consummate, quit Ghostscript

After Ghostscript completes, there are images named 'image#.png' for each page of the PDF in the same folder.

After Ghostscript completes, there are images named 'image#.png' for each page of the PDF in the same folder.

The quality of the generated PNG images is excellent, since PNG images do not have paradigm compression. To reduce the filesize of the generated images, consider using JPEG as the output format. Y'all can then specify the output quality of the image to reduce its filesize:

          gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=prototype%i.jpg -dJPEGQ=ninety -dPDFFitPage -g768x1024 -q example.pdf -c quit

The filesize will be significantly less, but the quality will accept a striking. If y'all only want to convert certain pages, you tin can specify the start and stop page numbers. To improve quality without really affecting filesize, yous can set how many bits should be used for rendering text and images. To improve functioning, you lot can fix the corporeality of rendering threads to be used.

          gs -dNumRenderingThreads=4 -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=paradigm%i.jpg -dJPEGQ=90 -dPDFFitPage -dTextAlphaBits=four -dGraphicsBits=4 -g768x1024 -q example.pdf -c quit

Get the Total Number of Pages

To return the total number of pages in a PDF, use the post-obit control:

          gs -q -dNODISPLAY -c "(instance.pdf) (r) file runpdfbegin pdfpagecount = quit"

Extract PDF Page Text

To extract a PDF'due south page text content, enter the post-obit command:

          gs -dNOPAUSE -sDEVICE=txtwrite -dFirstPage=1 -dLastPage=10 -sOutputFile=output.txt -q instance.pdf -c quit

This will extract the text content of pages 1 to 10 and output it into a textfile named 'output'.

A text file containing the extracted text content is generated in the same folder.

A text file containing the extracted text content is generated in the same folder.

If y'all want to ouput the text content to the Final window instead, use a nuance as the output filename:

          gs -dNOPAUSE -sDEVICE=txtwrite -dFirstPage=1 -dLastPage=10 -sOutputFile=- -q example.pdf -c quit

Using Ghostscript with PHP

The examples shown above are all console commands entered into the Terminal window. How would you then be able to apply Ghostscript in a PHP script? By making utilize of the following line of PHP code:

          $output = shell_exec('gs -dNOPAUSE -sDEVICE=png16m -sOutputFile=/path/to/output/image%i.png -dPDFFitPage -g768x1024 -q /path/to/input/instance.pdf -c quit');

This will do the same as the showtime example and the output will be stored in the $output variable. Storing the output into a variable is especially useful if y'all want to extract a PDF folio's text content:

          $extracted_text = shell_exec('gs -dNOPAUSE -sDEVICE=txtwrite -dFirstPage=1 -dLastPage=10 -sOutputFile=- -q instance.pdf -c quit');

To Conclude

As with nigh things in life, with great power comes great responsibleness. Ghostscript is a very powerful tool that tin be used for various format conversions such equally from PDF page to paradigm and vice versa. It tin can as well be used to translate a PDF page's description linguistic communication in order to excerpt text content or go the total page count. There are many more powerful ways in which you can use Ghostscript. That is where the responsibleness part comes in: it is your responsibility to head on over to the Ghostscript documentation and spend hours upon hours fiddling with commands, tweaking options and discovering absolutely everything you tin can maybe do with Ghostscript!

Source: https://www.grenadeco.com/tutorial/introduction-to-ghostscript/

Posted by: boylesmaland.blogspot.com

0 Response to "How To Use Ghostscript In Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel