Merge Multiple Jpgs into Single PDF in Linux

Merge multiple JPGs into single PDF in Linux

The problem is because your shell is expanding the wildcard in a purely alphabetical order, and because the lengths of the numbers are different, the order will be incorrect:

$ echo *.jpg
1.jpg 10.jpg 100.jpg 101.jpg 102.jpg ...

The solution is to pad the filenames with zeros as required so they're the same length before running your convert command:

$ for i in *.jpg; do num=`expr match "$i" '\([0-9]\+\).*'`;
> padded=`printf "%03d" $num`; mv -v "$i" "${i/$num/$padded}"; done

Now the files will be matched by the wildcard in the correct order, ready for the convert command:

$ echo *.jpg
001.jpg 002.jpg 003.jpg 004.jpg 005.jpg 006.jpg 007.jpg 008.jpg ...

Merge / convert multiple PDF files into one PDF

I'm sorry, I managed to find the answer myself using google and a bit of luck : )

For those interested;

I installed the pdftk (pdf toolkit) on our debian server, and using the following command I achieved desired output:

pdftk file1.pdf file2.pdf cat output output.pdf

OR

gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf file1.pdf file2.pdf file3.pdf ...

This in turn can be piped directly into pdf2ps.

Script to merge all image files from a folder inside a pdf

Trying to parse ls is wrong, as you've found out. Assuming you're using GNU versions of utilities as the linux tag usually implies, try something like:

#!/usr/bin/env bash

# Turn on case-insensitive globs and make ones that
# don't match anything expand to nothing
shopt -s nocaseglob nullglob

readarray -d '' -t images < <(printf "%s\0" *.jpg *.jpeg *.png *.gif *.tiff *.webp *.bmp | sort -Vz)
convert "${images[@]}" compilation_images.pdf

which uses the common trick of separating filenames with nul characters in order to safely handle whitespace when populating a bash array with sorted filenames.

How can I convert a series of images to a PDF from the command line on Linux?

Using ImageMagick, you can try:

convert page.png page.pdf

For multiple images:

convert page*.png mydoc.pdf

How to merge many PDF files into a single one?

You can use http://www.mergepdf.net/ for example

Or:

PDFTK http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

If you are NOT on Ubuntu and you have the same problem (and you wanted to start a new topic on SO and SO suggested to have a look at this question) you can also do it like this:

Things You'll Need:

* Full Version of Adobe Acrobat
  1. Open all the .pdf files you wish to merge. These can be minimized on your desktop as individual tabs.

  2. Pull up what you wish to be the first page of your merged document.

  3. Click the 'Combine Files' icon on the top left portion of the screen.

  4. The 'Combine Files' window that pops up is divided into three sections. The first section is titled, 'Choose the files you wish to combine'. Select the 'Add Open Files' option.

  5. Select the other open .pdf documents on your desktop when prompted.

  6. Rearrange the documents as you wish in the second window, titled, 'Arrange the files in the order you want them to appear in the new PDF'

  7. The final window, titled, 'Choose a file size and conversion setting' allows you to control the size of your merged PDF document. Consider the purpose of your new document. If its to be sent as an e-mail attachment, use a low size setting. If the PDF contains images or is to be used for presentation, choose a high setting. When finished, select 'Next'.

  8. A final choice: choose between either a single PDF document, or a PDF package, which comes with the option of creating a specialized cover sheet. When finished, hit 'Create', and save to your preferred location.

    • Tips & Warnings

Double check the PDF documents prior to merging to make sure all pertinent information is included. Its much easier to re-create a single PDF page than a multi-page document.

Merge multiple pdf pages into specific pdf page using pdftk

Only a little correction of your original proposal is needed:

pdftk A=source.pdf B=merged.pdf cat A1-45 B A46-end output final.pdf

You don't have to write "B1-18" to refer to the whole document. You can just use "B" to indicate an entire PDF. BTW, you can also use "end" if you want to refer to the last page, e.g. "B1-end"

"A64" in your example means "catenate page 64 of source document" but - if I understand it correctly - you want to put those 18 pages inside larger document and you don't want to lose any page of any of those two documents, right?

Then you need to catenate first 45 pages of the source document (which translates to "A1-45"), then 18 pages of the merged document ("B"), and finally all the pages from page 46 to the end of the source document ("A46-end").



Related Topics



Leave a reply



Submit