12

Using this library wkhtmltopdf, is it possible to merge 2 pdf files. I need to generate a report and merge the report with some attached documents.

Generation of report is done, converting from HTML to PDF. But I need to merge the resulting PDF with some other PDFs

user1236048
  • 5,542
  • 7
  • 50
  • 87
  • You can use [`PDFtk`](http://www.pdflabs.com/tools/pdftk-server/) which has all kinds of options for merging PDFs. – Arman H Sep 23 '13 at 03:53

2 Answers2

16

wkhtmltopdf doesn't provide support for merging PDF's. You can accomplish this using GhostScript. It's a pretty powerful tool and frequently used by Linux users.

You can use the following command:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.pdf in2.pdf

You can try these links for further reading:

In case you want it for Linux refer

EDIT: As stated by @nenotelp there are other tools available as well. The part that @nentelp mentioned is regarding the handling of bookmarks which is better with iText or iTextSharp. If this is your requirement you can certainly opt for other tools.

P.S. - Google Ghostscript. You will find tons of references.

Taoufik Mohdit
  • 1,910
  • 3
  • 26
  • 39
user2339071
  • 4,254
  • 1
  • 27
  • 46
  • 2
    This is true, BUT I would highly recommend iText or iTextSharp for the merge - wkhtmltopdf bookmarks are not your dads bookmarks, they don't play well with GhostScript. They don't play well with pdftk either. I know that you are about tho say something like "Ghostscript with pdfmark can easily deal with that" - it should, but it won't :P. – Joel Peltonen Sep 17 '13 at 10:28
  • 2
    Also see http://stackoverflow.com/a/3108884/694325 with an awesome answer regarding this topic. When I tested it, it did not work with wkhtmltopdf bookmarks. Also the TOC get screwed, but there's nothing you can do about that - wkhtmltopdf TOC anchors are not unique ARGFHH! I have complained about this into the bug system but Antialize is too busy and nobody else has stepped up yet because of the QT mess. – Joel Peltonen Sep 17 '13 at 10:31
4

You can combine multiple html files to one output pdf file with this library. For e.g if u have:

my-html-file-nr-1.html
my-html-file-nr-2.html
....
my-html-file-nr-n.html

You can simply:

wkhtmltopdf my-html-file-nr-* output.pdf
Andreew4x4
  • 469
  • 4
  • 18