Combine multi-page PDF files into a single PDF file using ImageMagick

I am trying to use ImageMagick (6.8.0) to combine several multi-page PDF files into one PDF file. This command:

$ convert multi-page-1.pdf multi-page-2.pdf merged.pdf 

Returns merged.pdf that contains the first page page with multiple pages 1.pdf and the first page with multiple pages 2.pdf. p>

This command:

 $ convert multi-page-1.pdf[2] multi-page-2.pdf[2] merged.pdf 

Returns a merged.pdf file containing a third page page with multiple pages 1.pdf and a third page with multiple pages 2.pdf.

I would like to combine .pdf to contain all the pages of each multi-page pdf. I still have not found a way to tell the convert command to use a range of pages, although I tried to add [0-1] and [0,1] at the end of the file names.

Interestingly, this ghostscript command (which I found through StackOverflow but cannot find) works the way I would like:

 $ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf multi-page-1.pdf multi-page-2.pdf 

The problem is that the ImageMagick 'convert' command accepts URLs as input, but ghostscript does not, and I need my program to enter the url, not file paths.

Is it possible to get the result of the above ghostscript command using ImageMagick?

+7
source share
2 answers

I asked this question in the company's internal forum, and it was concluded that there is no way to make the type of document merging that we would like to do with ImageMagick without first loading the local file system file.

For those of you who use Heroku, we use the Heroku β€œtmp” directory to save the file β€œlocally” at the production and production stage: https://devcenter.heroku.com/articles/read-only-filesystem

As soon as we save the file in 'tmp', we will iterate over each pdf page and save them separately. We will find the number of pages in PDF format using the "pdf-reader".

EDIT:

Here is the paperclip user processor that I wrote to handle this (all files are delivered to the tmp directory in advance):

https://gist.github.com/jessieay/5832466

0
source

Why aren't you using pdfunite ? ( source )

0
source

Source: https://habr.com/ru/post/947696/


All Articles