In certain situations you would like to create a PDF based on multiple URLs or on multiple HTML files. Or you may want to have a different layout for a certain amount of the pages then for the rest of PDF. In that case the new merge functionality comes in handy.
The PDF merge functionality, which is now available in our HTML to PDF SDK, allows you to merge pages from several PDFs together into one new PDF. You can select pages from any PDF that you’ve previously created with our service and we’ll add them together into a single PDF.
This allows you to make e-books, brochures, study books and many more documents with different page formats & sizing throughout the PDF. And all this time you can still do all the things to the individual PDFs that you’re used to do, which means you can control each individual PDF’s layout, header, footer, watermark etc etc. You can even encrypt each PDF individually, but of course you should specify the encryption that you want for the final document.
How does PDF merging work?
The principle of our merging service is quite easy. You just keep on doing what you were already doing before and whenever you want to merge documents, you just execute the ‘MergeFromCache’ command. This tells us how many of your previously created PDFs we have to take and which pages you want to merge from those PDFs.
As you understand from the function title, we take the PDFs from our cache. We currently cache your PDFs for 2 hours and during this time you are free to join them into a new PDF whenever you want. We do not charge you any credits for this, cause you already used credits to do the conversion and we just add merging as an additional service.
Example code
Let’s take a small example to see what kind of things you can do with the merge functionality. Suppose you wanted to take HTML from two different sources, use a portrait layout for one source and a landscape layout for the other. Let’s also assume that the second document is something you’d like to watermark with ‘DRAFT’.
Sounds complicated? With our SDK it’s just a matter of a few lines!
Here we go.
$pdf = new Htm2PdfApi($userid, $apikey);
$pdf->SetPageSize('Letter'); // 'Letter' is our page format
$pdf->SetPageOrientation('Portrait'); // first document is in 'Portrait'
$pdf->CreateFromURL('http://www.google.com'); // it's the Google homepage
$pdf->SetPageOrientation('Landscape'); // second one is in 'Landscape'
$pdf->SetWatermarkLocation(200,200); // watermark @ position 200, 200
$pdf->SetWatermarkTransformation(45, 0.5); // rotate and semi-opague
$pdf->SetWatermarkText('DRAFT','arial','#FF0000',80); // write 'DRAFT' in red
$pdf->CreateFromURL('http://www.bbc.co.uk'); // it's the BBC homepage
$pdf->MergeFromCache(2); // now let's merge these two
$pdf->Display(); // and display the merged PDF!
If you would do this you’d get the following three documents (note that the SDK allows you to use all three of them if you wanted to).
PDF 1 | PDF 3 | PDF 3 |
Limitations
As stated earlier, you can only merge PDFs together, which you created with the SDK in the last two hours, because currently we cache those files only for 2 hours. Exceptions can be made, so feel free to contact us when you want to be in the exception category.
Another limitation is that we really merge the PDFs. We can’t do any additional actions on the result (except for encryption). So we can not fit the resulting document to a single page or apply a watermark to it. You will get exactly what you created before, but just cut and pasted together. So if you wanted to have a watermark you have to watermark the individual PDFs you created before.
The only thing we CAN do for you is to apply encryption, password protection and rights management to the resulting document. So in that way you can make sure that your merged document still needs to have a document open password or is protected against printing.
Conclusion
This article showed you the new merge functionality, which is now live for the HTML to PDF SDK. It gives you a lot of power in creating compound documents and e-books and we feel it’s a valuable addition to our service. We are currently planning to add this (in limited form) to our HTML to PDF API as well, so please stay tuned if you’re interested in that service!