简体   繁体   中英

Convert PDF to PCL using Ghostscript 9.15

Requirement is to convert PDF to PCL with a macro embedded (currently testing this on Windows, however I will need to use this runtime in the application and print it from UNIX). The macro will be used later in another document to embed this cropped image and printed on one single page. I will be using PCL escape codes to call the MacroNumber and then the image will be printed. (You can consider this as a logo image.)

I am able to convert the PDF with whitespace to just the PDF without any whitespace by using CropBox.

"c:\progra~1\gs\gs9.15\bin\gswin64.exe" -o _sourcePDFcropped.pdf \
    -sDEVICE=pdfwrite -c "[/CropBox [1 140 320 650] /PAGES pdfmark" \
    -f _sourcePDF.pdf

However, when I convert this _sourcePDFcropped.pdf to PCL, this still adding whitespace.

"c:\progra~1\gs\gs9.15\bin\gswin64c.exe" -dBATCH -dNOPAUSE \
   -sDEVICE=pxlcolor -g100x200 -sOutputFile=_sourceFedGroundCroppedTest.pcl \
   -f _sourceFedGroundCropped.pdf

I tried using MKPCL and it does the job. Because it doesn't have much support, I am trying to use Ghostscript.

MKPCL.EXE -c4 -t -m 100 -p Image.jpg Image.MAC

I also tried ImageMagick which internally uses Ghostscript. So I am guessing, if I use the right switches in GS, I should be able to achieve my goal.

Input PDF File: Click Here

PS: I have seen other PDF to PCL queries on Stackoverflow, others are more of straight forward PDF to PCL. Mine is to crop the PDF and output should be PCL.

Question continued: Link

I processed the sample input PDF with the following command line, using a self-compiled Ghostscript v9.16 (unreleased, from current GhostPDL GIT sources):

gs -o -                                          \
   -sDEVICE=pdfwrite                             \
   -c "[/CropBox [1 140 320 650] /PAGES pdfmark" \
   -f source.pdf                                 \
                                                 \
| gs -o tst.pcl                                  \
    -sDEVICE=pxlcolor                            \
    -dUseCropBox                                 \
    -f -

(As you may well have noticed, I'm connecting 2 different Ghostscript commands through a pipe in order to save writing a temporary PDF file to disk.)

If you want to do the same on Windows, the command line in a cmd.exe /DOS box would be:

gswin64c.exe -o -                                ^
   -sDEVICE=pdfwrite                             ^
   -c "[/CropBox [1 140 320 650] /PAGES pdfmark" ^
   -f source.pdf                                 ^
                                                 ^
| gswin64c.exe -o tst.pcl                        ^
    -sDEVICE=pxlcolor                            ^
    -dUseCropBox                                 ^
    -f -

Then I opened it with the self-compiled PCL viewer (also from GhostPDL sources), pcl6 :

pcl6 tst.pcl

This is a screenshot showing the pcl6 window:

pcl6屏幕截图

As KenS also pointed out: it is important to use -dUseCropBox when processing the cropped PDF intermediate data!

Adding a CropBox doesn't really do much, it leaves the PDF exactly the same, but adds a CropBox entry for the page. GS will usually use the MediaBox, not the CropBox, so adding a CropBox to a PDF has no effect.

You could try adding -dUseCropBox . If the white space you think is being added is in fact present in the original PDF, but masked by the CropBox, then using -dUseCropBox will have GS use the CropBox when rendering the PDF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM