I have got some pdf documents that need to be printed to A4 paper.
I know there are options in my pdf reader (evince) to scale the document to fit the exact paper size it's printed to, and that works fine with printing to a file, too.
So there must be a CLI-solution to get this job done, right?
Whenever I want a pdf to be 2 pages on one sheet, I use
pdfnup in.pdf --nup 2x1 --outfile out.pdf
But that doesn't work – in.pdf is not scaled to fit, instead it's too tiny compared to the output of evince printed to a file. So how can I scale it before (or while) using pdfnup? It would be great if that worked for any input format, not just for A5. Thanks in advance for any answer!
Regards, LDer
Oh boy, I just figured something out on my own. Sorry for bothering anyone!
gs -o out.pdf -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dCompatibilityLevel=1.4 in.pdf
Related
I am using (PDF)LaTeX to make a document, and I also need to embed already existing PDF documents in it. The problem is that I have PDF documents in several different page sizes (letter, a4, etc) and I want to compile all of them into a single b5 PDF document.
If I use the pdfpages package from CTAN, all hyperlinks from the original PDFs are removed. So I tried to do it with GhostScript.
This sounds like something normal to do but I have failed to find a working solution.
I have, in the meanwhile, read a few question and answers, but failed to figure out what I am doing wrong and what I am missing.
This doesn't seem to address my problem of scaling.
Neither does that.
This seems to go in the right direction but I couldn't make use of the information :-(.
To make the problem easier, let's just try to resize a single PDF so that:
its contents are scaled to fit the page
the new page has the size I want
Sounds easy, and it is easy to do, for example with pdfjam:
pdfjam --outfile b5-foo.pdf --paper b5paper foo.pdf
Now the problem with this is that pdfjam throws away hyperlinks. From its website:
A potential drawback of pdfjam and other scripts based upon it is that any hyperlinks in the source PDF are lost.
This must be because it seems to use pdfpages mentioned above.
Unlike pdfjam, GhostScript keeps hyperlinks. However, it either:
crops the original when I downscale; or
does not put the scaled content on a page of the size I need -- instead, I get a page that seems to be scaled down, while keeping the original aspect ratio.
This is what I have installed:
$ gs --version
9.21
(Installed on Linux)
This is how I can use GhostScript to crop the content:
gs -dBATCH -dNOPAUSE \
-sDEVICE=pdfwrite -dFIXEDMEDIA -sPAPERSIZE=isob5 \
-o b5-foo.pdf foo.pdf
... and here is how I can use -dPDFFitPage to scale the content but also keep the aspect ratio of the original page size:
gs -dBATCH -dNOPAUSE \
-sDEVICE=pdfwrite -dFIXEDMEDIA -sPAPERSIZE=isob5 -dPDFFitPage \
-o b5-foo.pdf foo.pdf
To be even clearer: I seem to get a page that is scaled so that it would fit inside the b5 I am asking for, but it is not b5: it still has the H/W ratio the original (letter) had!
I'd be happy if this can be done just using switches but if I need to use PostScript that's perfectly fine.
The solution seems to be to use -dPSFitPage instead of -dPDFFitPage. This might have something to do with the PDF files that I am trying to resize. Unfortunately, I cannot share those :-(. When I tried to reproduce this with files that I generated and the problem does not reproduce. I don't know why this is or how I should have known it.
To summarize, using PDF files for both input and output:
-dFitPage and -dPDFFitPage give me scaled pages with the original aspect ratio
-dPSFitPage gives me scaled content on the page size I request with -sPAPERSIZE="$PAPERSIZE"
This seems to go against what the documentation says.
I don't speak good english, but I hope anyone can help me on this one...
I spent several days on this but I can't figure out on my own. Here's the deal:
I Have 4000+ PDF documents, with TrimBox margins, each one with 16 pages, color.
I needed to batch print them:
Print pages 1-10 using the paper on tray 3;
Print pages 11-15 using the paper on tray 4, two copies uncollated.
Print page 16 using the paper on tray 3.
I'm using an Kyocera 7550ci, the PPD is here.
I have installed GhostScript 9.19, and also gsview with gsprint. Windows 7 SP1.
When I first tried to do anything at all, didn't know ghostscript or how to use it, but doing some reading I managed to "kind of" solve the problem. I duplicated the printer on Windows control panel, setted each one with the configurations I wanted and did the following command on GSPRINT:
gsprint -printer "Kyocera TASKalfa 7550ci KX" -color -dUseTrimBox -dFitPage -from 1 -to 10 s_file0001.pdf
gsprint -printer "ALT Kyocera" -color -dUseTrimBox -dFitPage -from 11 -to 15 -copies 2 s_file0001.pdf
gsprint -printer "Kyocera TASKalfa 7550ci KX" -color -dUseTrimBox -dFitPage -from 16 -to 16 s_file0001.pdf
(I setted TASKalfa 7550ci default driver to use tray 3, and ALT Kyocera to use tray 4 and uncollate).
It worked, but was painfully slow both to Windows process, and the printer to process. I soon realised GSPRINT is slow because it has to render the whole image to bitmap, and started to see if I could use pure GhostScript to do the work.
gswin32c -dBATCH -dNOPAUSE -q -dUseTrimBox -dFitPage -dFirstPage=1 -dLastPage=10 -sDEVICE=mswinpr2 -sOutputFile="%printer%Kyocera TASKalfa 7550ci KX" -f test.pdf
gswin32c -dBATCH -dNOPAUSE -q -dUseTrimBox -dFitPage -dFirstPage=11 -dLastPage=15 -sDEVICE=mswinpr2 -sOutputFile="%printer%ALT Kyocera" -f test.pdf
gswin32c -dBATCH -dNOPAUSE -q -dUseTrimBox -dFitPage -dFirstPage=16 -dLastPage=16 -sDEVICE=mswinpr2 -sOutputFile="%printer%Kyocera TASKalfa 7550ci KX" -f test.pdf
But I'm still with alot of problems... I'm frustrated that I couldn't get it to work even trying really hard to read manuals and search around.
Using mswinpr2 is still really slow, gives me wrong colors, and can't figure out how to select the paper tray.
Using any included PCL drivers, altrought was fast and managed to select the correct tray using dMediaPosition, there's only Black and white drivers...
Using pdfwrite, don't correct scale the TrimBox to fit the whole page, and can't select correct tray.
Using ps2write, can't select tray and messes up with the page position.
I'm lost. someone can give me some directions? Also, there's some way to send everythign as one file to the printer?
Thank you ALL!
---EDIT---
Thank you both for the answers!
I managed to make it work:
gswin32c -dBATCH -dNOPAUSE -q -dPDFFitPage -dUseTrimBox -dFirstPage=1 -dLastPage=10 \
-dMediaPosition=7 -sDEVICE=pxlcolor \
-sOutputFile="%printer%Kyocera TASKalfa 7550ci KX" -f in.pdf
gswin32c -dBATCH -dNOPAUSE -q -dPDFFitPage -dUseTrimBox -dFirstPage=11 -dLastPage=15 \
-dMediaPosition=5 -sDEVICE=pxlcolor -dNumCopies=2 \
-sOutputFile="%printer%Kyocera TASKalfa 7550ci KX" -f in.pdf
gswin32c -dBATCH -dNOPAUSE -q -dPDFFitPage -dUseTrimBox -dFirstPage=16 -dLastPage=16 \
-dMediaPosition=7 -sDEVICE=pxlcolor \
-sOutputFile="%printer%Kyocera TASKalfa 7550ci KX" -f in.pdf
The only thing is that the page doesn't scale correctly on pxlcolor (it does on ljet4, but it's black and white).
I'm almost there! Thanks ^^. If anyone knows about this problem, I would appreciate.
You have asked a lot of questions, all at once, that's not really a good way to get helpful answers. In addition you haven't really been too clear about some of the problems.
1) If you want to use the TrimBox for the media size then you have to tell Ghostscript you want to use the TrimBox, you do that by -dUseTrimBox, no matter what device you want to use.
2) The mswinpr2 device works by creating a Windows DeviceContext for the printer, rendering the input to a (RGB) bitmap, then blitting the bitmap to the DeviceContext and telling it to print itself. This is slow because it will involve rendering a large bitmap (size dependent on printer resolution) to memory and then sending that large bitmap to the device.
Its one great advantage is that it will work no matter what printer you have.
GSPrint uses a 'similar' but somewhat different technique and is claimed to be faster.
Note that both these devices use the default settings of the printer which probably won't work for your complex needs.
Colour management is, of course, up to Windows in this case, but if your original PDF is specified in say CMYK then this will involve conversions CMYK->RGB->CMYK which is bound to cause colour differences.
3) There are colour PCL devices available in Ghostscript, eg the cdeskjet device.
4) pdfwrite will use the TrimBox if you select -dUseTrimBox. Since it creates a PDF file its rather hard to see how it could 'select correct tray'. If you are sending the PDF file to the printer, then you could simply have started with the original PDF file. PDF files cannot contain device-dependent criteria, such as tray selection.
5) ps2write in its current incarnation will allow you to add device-specific operations, see ghostpdl/doc/VectorDevices.htm (also available on the ghostscript.com website), section 6.5 "PostScript file output" and look for the PSDocOptions and PSPageOptions keys. You could use the PSPageOptions array to introduce individual media selection commands to each page. I have no idea what you mean by 'messes up the page position', however yet again if you do not select -dUseTrimBox then it will not be using the TrimBox........
Oh, and if you want to 'scale the TrimBox to fit the whole page' (which you only mention regarding pdfwrite) then you will have to set up a fixed media of the size you want the page scaled to (-dFIXEDMEDIA, -dDEVICEHEIGHTPOINTS= and -dDEVICEWIDTHPOINTS=), select -dUseTrimBox and -dPDFFitPage.
There is no easy way to do this. While PDF itself does not provide a facility to switch the paper trays your need to convert this stream to another PDL. PostScript is a good choice.
While converting to PostScript you can inject PostScript tray switching commands like those found in PPD:
<< /ManualFeed false >> setpagedevice statusdict begin 5 setpapertray end
On Windows platform you have choices on the implementation:
Alter the PPD to make it injecting the PostScript code before every page. The code should maintain a page counter and execute tray switching commands accordingly.
Buy a third-party software providing this capability.
Extend the printer driver with the DLL injecting the PostScript code.
The first may not work with your printer diver. Then you can try to inject a PostScript code at the beginning of the job. The code should override showpage extending it with the capability described in the first option.
The same code overloading showpage you could inject in PostScript interpreter startup sequence if you had an access to internals of the controller.
I'm using GPL Ghostscript 9.07 (2013-02-14) on OS X (10.8.4) to convert many PDFs to PNGs.
It works fine except for one of the PDFs which turns into a PNG with jagged edges. In other words, Ghostscript turns off antialiasing for that particular PDF for some reason.
The PDF in question.
The output:
In other cases it works fine (sample: pdf -> png).
I use this command:
gs -dNOPAUSE -dBATCH -dPDFFitPage -sDEVICE=pngalpha -g200x150 -sOutputFile=01.png 01.pdf
Is it possible to force Ghostscript to use antialiasing for that PDF?
Any tips are appreciated.
This worked for me:
gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150 -sOutputFile=foo-%d.jpg foo.pdf
Source: ImageMagick convert pdf to jpeg has poor text quality after upgrading ImageMagick version to 6.7.8
The above would work for a JPG; for PNG, replace the -sDEVICE option with your choice, example: -sDEVICE=png16m
Source: http://ghostscript.com/doc/current/Devices.htm
You can try -dGraphicsAlphaBits= with values 1,2 or 4 which may or may not make a difference. It made some improvement for me, but its a small graphic at low resolution with an awkward curve, so not so much as might be expected.
Or you can use one of the anti-aliasing devices (eg tiffscaled) which are more flexible. There is no anti-aliased device for PNG output but it would be trivial to convert TIFF to PNG.
By the way, your PDF file specifically turns off anti-aliasing on the components:
8 0 obj
<</AntiAlias false/ColorSpace/DeviceCMYK/Coords[0.0 0.0 1.0 0.0]/Domain[0.0 1.0]/Extend[true true]/Function 10 0 R/ShadingType 2>>
You might like to try and see what happens if you change AntiAlias to true, though I doubt this will have an effect as I'm pretty sure the aniti-aliasing is applied to the internal rendering of the shading, not the edgses.
You can try -dDOINTERPOLATE which uses a Mitchell filter function to scale the contributions for each output pixel
I have many (around 1000) multiple-page PDFs for a program I am writing.
The problem is that many of them are inconsistent about page size, even within the same document at times. Does anyone know of a way I could programmatically go through the files and resize the pages to what I want? This can be in any language.
I can accomplish this in Adobe Acrobat Pro, but there are so many that would end up taking a long, long time. The only way I can get it to resize there is to add a background from a file, and then choosing the file i want to resize.
Generally, PDFtk is a good fit for this kind of problems. It will let you pull everything apart, and reorder/resize/modify pages on the command line.
I had a similar problem and could easily solve it with PDF split and merge, a Java based toolkit for editing PDF files.
You can resize a PDF with a command line tool, Ghostscript.
Assuming you want to resize a PDF to 306x396 points (which would give you a quarter of a letter sized pages), do it like this:
gs \
-o 306x396-points.pdf \
-sDEVICE=pdfwrite \
-g3060x3960 \
-dPDFFitPage \
-dUseCropBox \
input.pdf
Note that the -g.... dimensions are in pixels. Because Ghostscript internally computes with 720 PPI by default, these are increased by a factor of 10 as compared to the sizes in points.
For Windows, use gswin32c.exe or gswin64c.exe instead of gs.
I need to shrink some large PDFs to print on an 8.5x11 inch (standard letter) page. Can ImageMagick/Ghostscript handle this sort of thing, or am I having so much trouble because I'm using the wrong tool for the job?
Just relying on the 'shrink to page' option in client-side print dialogs is not an option, as we'd like for this to be easy-to-use for the end users.
I would not use convert. It uses Ghostscript in the background, but is much slower. I'd use Ghostscript directly, since it gives me much more direct control (and also some control over settings which are much more difficult to achieve with convert). And for convert to work for PDF-to-PDF conversion you'll have Ghostscript installed anyway:
gs \
-o /path/to/resized.pdf \
-sDEVICE=pdfwrite \
-dPDFFitPage \
-r300x300 \
-g2550x3300 \
/path/to/original.pdf
The problem with using ImageMagick is that you are converting to a raster image format, increasing file size and decreasing quality for any vector elements on your pages.
Multivalent will retain the vector information of the PDF.
Try:
java -cp Multivalent.jar tool.pdf.Impose -dim 1x1 -paper "8.5x11in" myFile.pdf
to create an output file myFile-up.pdf
ImageMagick's mogrify/convert commands will indeed do the job. Stephen Page had just about the right idea, but you do need to set the dpi of the file as well, or you won't get the job done.
Assuming you have a file that's 300 dpi and already the same aspect ratio as 8.5 x 11 the command would be:
// 300dpi x 8.5 -2550, 300dpi x 11 -3300
convert original.pdf -density "300" -resize "2550x3300" resized.pdf
If the aspect ratio is different, then you need to do some slightly trickier cropping.
The Ghostscript approach worked well for me. (I moved my file from my Windows PC to a Linux computer and ran it there.) I made one small change to the Ghostscript command because the Ghostscript resize command above completely fills an 8.5 by 11 inch page. My printer cannot print to the edge, though, so several milllimeters along each page edge were lost. To overcome that problem, I scaled my PDF document to 0.92 of a full 8.5 by 11 inches. That way I saw everything centered on the page and had a slight margin. Because 0.92 * (2550x3300) = (2346x3036), I ran the following Ghostscript command:
gs -sDEVICE=pdfwrite \
-dPDFFitPage \
-r300x300 \
-g2346x3036 \
/home/user/path/original.pdf \
-o /home/user/path/resized.pdf
If you use Insert > Image... in LibreOffice Writer to insert a PDF, you can use direct manipulation or its Image Properties to resize and reposition the PDF, and when you File > Export as... PDF the PDF remains vectors and text. Interestingly when I did this with a PDF invoice the PDF exported from LO is smaller than the original, but the Linux pdfimages command-line utility suggests LO preserves any raster images within the original PDF.
However, you want something easier-to-use for your end users than the print dialog's "Shrink to page" option. There are tools like Adobe Acrobat that lay out PDFs to form print jobs that are PDFs; I don't know which ones have a simple "Change the bounding box and scale to letter-size". Surprisingly the do-it-all qpdf tool lacks this feature.