Merge / convert multiple PDF files into one PDF

2019-01-02 16:19发布

How could I merge / convert multiple PDF files into one large PDF file?

I tried the following, but the content of the target file was not as expected:

convert file1.pdf file2.pdf merged.pdf

I need a very simple/basic command line (CLI) solution. Best would be if I could pipe the output of the merge / convert straight into pdf2ps ( as originally attempted in my previously asked question here: Linux piping ( convert -> pdf2ps -> lp) ).

17条回答
时光乱了年华
2楼-- · 2019-01-02 16:53

Use PDF tools from python https://pypi.python.org/pypi/pdftools/1.0.6

Download the tar.gz file and uncompress it and run the command like below

python pdftools-1.1.0/pdfmerge.py -o output.pdf -d file1.pdf file2.pdf file3 

You should install pyhton3 before you run the above command

This tools support the below

  • add
  • insert
  • Remove
  • Rotate
  • Split
  • Merge
  • Zip

You can find more details in the below link and it is open source

https://github.com/MrLeeh/pdftools

查看更多
不流泪的眼
3楼-- · 2019-01-02 16:53

Here's a method I use which works and is easy to implement. This will require both the fpdf and fpdi libraries which can be downloaded here:

require('fpdf.php');
require('fpdi.php');

$files = ['doc1.pdf', 'doc2.pdf', 'doc3.pdf'];

$pdf = new FPDI();

foreach ($files as $file) {
    $pdf->setSourceFile($file);
    $tpl = $pdf->importPage(1, '/MediaBox');
    $pdf->addPage();
    $pdf->useTemplate($tpl);
}

$pdf->Output('F','merged.pdf');
查看更多
裙下三千臣
4楼-- · 2019-01-02 16:54

I second the pdfunite recommendation. I was however getting Argument list too long errors as I was attempting to merge > 2k PDF files.

I turned to Python for this and two external packages: PyPDF2 (to handle all things PDF related) and natsort (to do a "natural" sort of the directory's file names). In case this can help someone:

from PyPDF2 import PdfFileMerger
import natsort
import os

DIR = "dir-with-pdfs/"
OUTPUT = "output.pdf"

file_list = filter(lambda f: f.endswith('.pdf'), os.listdir(DIR))
file_list = natsort.natsorted(file_list)

# 'strict' used because of
# https://github.com/mstamy2/PyPDF2/issues/244#issuecomment-206952235
merger = PdfFileMerger(strict=False)

for f_name in file_list:
  f = open(os.path.join(DIR, f_name), "rb")
  merger.append(f)

output = open(OUTPUT, "wb")
merger.write(output)
查看更多
公子世无双
5楼-- · 2019-01-02 16:59

pdfunite is fine to merge entire PDFs. If you want, for example, pages 2-7 from file1.pdf and pages 1,3,4 from file2.pdf, you have to use pdfseparate to split the files into separate PDFs for each page to give to pdfunite.

At that point you probably want a program with more options. qpdf is the best utility I've found for manipulating PDFs. pdftk is bigger and slower and Red Hat/Fedora don't package it because of its dependency on gcj. Other PDF utilities have Mono or Python dependencies. I found qpdf produced a much smaller output file than using pdfseparate and pdfunite to assemble pages into a 30-page output PDF, 970kB vs. 1,6450 kB. Because it offers many more options, qpdf's command line is not as simple; the original request to merge file1 and file2 can be performed with

qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf
查看更多
余生无你
6楼-- · 2019-01-02 17:02

After searching many of the available pdf libraries, the only thing which worked perfectly for me is

https://libraries.io/npm/easy-pdf-merge

It requires Java 6 or above to be installed but works perfectly. Pdftk is full of bugs.

var merge = require('easy-pdf-merge');

merge(source_files,dest_file_path,function(err){

        if(err)
        return console.log(err);

        console.log('Success');

});

Note: This isn't cmd but you can make it run with commands like accepting filenames as command line arguments.

查看更多
有味是清欢
7楼-- · 2019-01-02 17:03

If you want to convert all the downloaded images into one pdf then execute

convert img{0..19}.jpg slides.pdf

查看更多
登录 后发表回答