39

I have a set of images stored as separate PDF files. One image per file. Each image takes up one PDF page.

I need to re-scale all of these images, preferably at the command line, so that the image content and the page dimensions of the PDFs are both scaled relative to their original sizes/dimensions. For example: scale all the images by 50% should shrink the size of the image and the dimensions of the page.

Already tried:

  • pdfpages + latex: will re-scale the image but dimensions of the pages stay the same.
  • pdfjam: same problem; can re-scale but page dimensions want to be letterpaper or a4.
  • convert (imagemagick): converts to raster, which I don't want.
  • ghostscript: seems to scale based on absolute new page size, and I need relative page size.

I know one of these must work. I can't figure out where I've gone wrong. I'm on Mac but a Linux solution would work just as well.

7 Answers 7

27

pdfjam works for this.

pdfjam --outfile out.pdf --paper a5paper in.pdf
6
  • Yes, and you can also just add --landscape to do exactly that. Sep 24, 2020 at 11:23
  • 1
    I wanted to really just scale both dimensions by some factor. In order to keep the initial image ratio you need to specify the explicit target size, e.g. --papersize '{16in,10in}' for some 16:10 ratio. Otherwise you might end up with unexpected whitespace around the image. You could extract the initial size as suggested here.
    – bixel
    Apr 9, 2021 at 10:38
  • 1
    Note that while this is potentially useful for many use cases, this example usage does *not* answer the question as-asked (and indeed, is listed as a non-workable solution). That said, it seems like pdfjam can (now?) mostly do this, as it does have a --scale option. That said, that option does not auto-scale the page size, too, as was requested in the original question. So, very useful for many, no doubt, but not strictly a valid answer to this question as asked... though could perhaps be adapted to be one??
    – lindes
    Jun 17, 2022 at 20:53
  • For US "letter" paper sizes (8.5 x 11 inches), use: pdfjam --outfile out.pdf --paper letter in.pdf Jun 27, 2022 at 21:53
  • @lindes can you detail why pdfjam does not answer the question? OP complains that "can re-scale but page dimensions want to be letterpaper or a4", but he seems to ignore the fact you can use custom page size with --papersize option, as @bixel pointed out Sep 16, 2023 at 5:10
26

I think I found one: http://community.coherentpdf.com/

cpdf -scale-page "0.5 0.5" in.pdf -o out.pdf

9
  • 6
    Yes thanks. I use cpdf -scale-to-fit "210mm 210mm" in.pdf -o out.pdf and it works great
    – maxbellec
    Feb 22, 2017 at 10:25
  • 3
    could not find cpdf in ubuntu repos but for me pdfjam as per this answer did the trick.
    – cardamom
    Aug 28, 2017 at 11:15
  • I used pdfposter as per this answer. Jun 12, 2018 at 8:49
  • 1
    @young_souvlaki, there's an online user manual with detailed examples
    – Sam
    Jul 22, 2022 at 22:39
  • 1
    As far as I can see, cpdf doesn't scale the images, only the page settings of the PDF. Aug 11, 2022 at 1:47
2

The following will do what you want:

https://github.com/tavinus/pdfScale

NB: As at 2021-07-14, there is no way to specify an arithmetic scale factor, but as pdfScale does accept custom page sizes, the following posix shell script overcomes that limitation:

#!/bin/sh
inFile=yourFilenameHere.pdf
scalePercent=50
# NB: 50% linear scaling takes A4 to A6
# For 50% area scaling (A4 to A5), use scalePercent=70
oldSize=$(pdfinfo $inFile | grep '^Page size')
oldWidth=$(echo "$oldSize" | awk '{print $3}')
oldHeight=$(echo "$oldSize" | awk '{print $5}')
newWidth=$(($oldWidth * scalePercent / 100))
newHeight=$(($oldHeight * scalePercent / 100))
pdfScale.sh -r "custom pts $newWidth $newHeight" $inFile
# default output filename is yourFilenameHere.CUSTOM.pdf

There's a page at https://ma.juii.net/blog/scale-page-content-of-pdf-files which explains the history behind the project - interesting stuff! HTH.

0

I also had a problem finding a tool that can do something like that; I want to change page size of and rotate a PDF document, but without scaling contents. And I just realized pdfedit (which is a GUI tool, however), can do it:

  • After the document is opened, go to Page/"Edit page metrics", and then edit the x,y positions of left upper/right lower corners, and then Change.
  • After that, can go to Page/"Rotate page 90 Degrees"

Note that:

0

Thanks, I tried pdfScale.sh and cpdf, and cpdf works for me. cpdf will stretch the cover and content to fill the entire page without extra white space left.

Here is the command, you need to calculate the scale ratio when use cpdf with the option -scale-page

cpdf -scale-page "x_ratio_not_size y_ratio_not_size" input.pdf -o output.pdf

I use a fish function cpdfscale which depends on pdfinfo and cpdf to convert pdf files for my apple devices.

Here is the cpdfscale.fish source code:

function cpdfscale
  # Get pdf file origin size
  set -l dim_x (pdfinfo $argv[3] | grep -a "Page size" | cut -d\: -f2 | cut -dx -f1 | sed 's/^[ \t]*//')
  set -l dim_y (pdfinfo $argv[3] | grep -a "Page size" | cut -d\: -f2 | cut -dx -f2 | cut -d' ' -f2 | sed 's/^[ \t]*//')
  echo Origin Page Size\t :\t $dim_x x $dim_y pts
  echo Scaled Page Size\t :\t $argv[1] x $argv[2] pts

  # Calc scale ratio
  set -l sx (math $argv[1] / $dim_x)
  set -l sy (math $argv[2] / $dim_y)
  echo Scaled Page Ratio\t :\t $sx x $sy

  # Set output file and convert
  if test (count $argv) -gt 3
    echo Scaled Output File\t :\t $argv[4]
    cpdf -scale-page "$sx $sy" $argv[3] -o $argv[4] &> /dev/null
  else
    set -l outfile (echo $argv[1]x$argv[2] - (basename $argv[3] .pdf).pdf)
    echo Scaled Output File\t :\t $outfile
    cpdf -scale-page "$sx $sy" $argv[3] -o $outfile &> /dev/null
  end
  echo "Scale finished"
end

Usage:

cpdfscale x_size_in_pts y_size_in_pts input.pdf [output.pdf].

if the output file is not specified, a new file name with origin name and new file page dimensions ${x_size_in_pts}x${y_size_in_pts} - ${origin_name}.pdf will be used.

The following command convert any pdf file for my iPad Air4 which has a screen with size 1640 x 2360 pts.

cpdfscale 1640 2360 origin.pdf scaled.pdf

The core command to get my work done is:

cpdf -scale-page "3.253968 4.173453" origin.pdf -o scaled.pdf

Here is the output:

>=>  cpdfscale 1640 2360 origin.pdf
Origin Page Size     :   504  x 565.479 pts
Scaled Page Size     :   1640 x 2360 pts
Scaled Page Ratio    :   3.253968 x 4.173453
Scaled Output File   :   1640x2360 - origin.pdf
Scale finished

>=>  cpdfscale 1640 2360 origin.pdf scaled.pdf
Origin Page Size     :   504  x 565.479 pts
Scaled Page Size     :   1640 x 2360 pts
Scaled Page Ratio    :   3.253968 x 4.173453
Scaled Output File   :   scaled.pdf
Scale finished
-1

What I wanted was to fit multiple pages into one page with vertical merge. The following helped:

# The following with merge every 3 pages into one (Every 3 pages are merged,  one below the other)
# You can also use --frame option for debugging. It will add each page on a frame.
pdfjam --nup 1x3 --outfile out.pdf in.pdf

Input:

p1
---
p2
---
p3
---
p4
---
p5

Output:

    p1
    p2
    p3
    ---
    p4
    p5

Refs:

-1

The best solution I've seen so far in the cpdf, you can use -scale-page to stretch both the page size and the page contents

specified_size=(200 300) # 200 pt x 300 pt

read -a input_size <<< "$(pdfinfo input.pdf | grep 'Page size:')" # get the input size

x_ratio=$(echo "scale=4; ${specified_size[0]} / ${input_size[2]}" | bc)
y_ratio=$(echo "scale=4; ${specified_size[1]} / ${input_size[4]}" | bc)

cpdf -scale-page "$x_ratio $y_ratio" input.pdf -o output.pdf
2
  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
    – DavidPostill
    Aug 15, 2023 at 11:03
  • @DavidPostill Ok. Aug 15, 2023 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .