2

I’ve been searching around a lot for this but wasn’t able to find a solution.

How can I generate a PDF with an arbitrary paper format (say A4) and fill its pages with as many copies of an image as possible?

I’m looking for a CLI solution, using programs like ImageMagick, Poppler, Ghostscript and such.

My use case: Changed password for the guests Wi-Fi in my office, wanted to send around a nice A4 PDF composed of a tight grid, each cell containing the network SSID and WPA-Key, to be printed, cut into pieces handed out to guests visiting.

Could not find a way to do it and resorted to paste the tiny images manually in a page using MS Word.

EDIT: how these tiny images looked like. Just typed into a text editor and made a screenshot of the text.

enter image description here

I’ll be changing this password once every 3 months, so I’d like an automated/scriptable solution. Doesn’t need to be perfect, but it needs to be quick.

2

Based on the image dimensions and such of the example JPG file you posted to your question, when I right click on it and select Save image as..., the saved file Image attributes list as. . .

  • Dimensions: 2850 x 1237
  • Width: 2850 pixels
  • Height: 1237 pixels

enter image description here

I was able to get 24 of these images squeezed into one image file using the below methods /commands with ImageMagick which I will outline below. While I will not provide the logic for copying "x" number of the original images to get the expected result, such tasks are trivial and I'll assume you won't need help or examples for that portion as such—if so though just let me know.


Detailed Steps. . .

Reminder: I did not provide logic for copying or deleting files where I mentioned those type of operations are needed and assume you can work that out but let me know otherwise.

  1. You will want to make multiple copies of the SSID and WPA-key image file in a /folder somewhere so you have a total of 6 images to run the first ImageMagick command against.

  2. When those 6 image files are in the /folder, you will run this command against them to create a new output file named out.jpg in my example:

    convert /folder/*.jpg -gravity center -append /output/out.jpg
    
  3. You will now delete the 6 original "SSID and WPA-key image" files and then copy over the new out.jpg to the /folder and then copy it until you have 4 total files in the /folder, and then run this command:

    convert /folder/*.jpg -gravity center +append /output/nout.jpg
    
  4. You can then delete the 4 copies of those files in /output that you copied over per #3 and then run this command against the newly created nout.jpg that was created with the above command to get it a page a4 formatted file:

    convert /output/nout.jpg -page a4 /output/print.jpg
    
  5. Now print the print.jpg image file and verify it looks good, cut, pass out, etc.

    enter image description here

Note: If your image dimensions and such for the starting image is different and doesn't scale out properly with the final result, you will want to play with the "x" number of images per each step I referenced, and then once you run the command against those images, you will want to open the correlated output files and check to see if you can figure out the "x" number those should be for each step. Testing this may be timely but is another trivial task that's as easy as that to figure out.


Further Resources

  • @zool I did play with montage options some and the images were blurry so I moved on to other methods. I also did not scale down the size of the original file to make smaller to see about fitting more smaller images into the one sheet but I assume this will be good enough for a basic starting point at least to get you by to work on automating into a script and such for your task. – Pimp Juice IT Oct 13 '18 at 3:23
1

You can do that with the help of Pandoc and output PDF.

First Method: multi-paste 1 line of text into Markdown, not images into MS Word

Instead of pasting the image into an MS Word document, you could also paste this line into a text file which you'll call my-markdown.md:

![](https://i.stack.imgur.com/l9TLd.jpg) { width="33%" }

This would put 3 images per row. Of course you could modify the width to any value you want. My guess is that you can do with 60 identical lines when using width="33%" and with 112 identical lines when using width="24%" (4 images per row).

Since you wanted 'arbitrary paper formats (say A4)', I'll give you an almost A4 example. You'll have to convert it to PDF like this:

pandoc          \
  -o images.pdf \
  -V geometry="paperwidth=580pt, paperheight=848pt, margin=10pt" \
     images.md 

Screenshot:

enter image description here

Second Method: do it all in one command line

Since I do not have a Windows system available right now, so I can confirm and test with Powershell or cmd.exe, I give you a Linux/macOS Bash example:

for i in {1..112} ; do
    echo '![](https://i.stack.imgur.com/l9TLd.jpg){ width="24%" }' ;
done \
|    \
pandoc -V geometry="paperwidth=580pt, paperheight=848pt, margin=10pt" \
       -o images2.pdf \
       -

The first part above writes the same line 112 times to <stdout>, but pipes it to Pandoc's <stdin> for further processing without writing out a Markdown file.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.