This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Multiple URLs into One PDF

gbweb wrote on 2012-01-18:
I have an application where a user adds pages to a "cart" like system and then generates a PDF based on what is in that cart. This means I need to create one PDF with multiple pages, multiple URLs.

When I try to do it in code (PHP)

$pdf .= $client->convertURI($url);

it only spits back the last page created.
gbweb wrote on 2012-01-18:
Correction, I echoed out the results and it does have each item (inside a foreach loop).

But when I set my headers and then echo the $pdf variable, it is only showing the last item, even though all of the code is there for both pages (or three, four, etc).

Is there something in the header calls that I'm doing to prevent multiple pages from being shown?

foreach($urls as $url){
$pdf[] = $client->convertURI($url);
}

header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"".$title.".pdf\"");
foreach($pdf as $item){
echo $item;
}
support wrote on 2012-01-18:
You are echoing a sequence of individual PDF files. However, merging multiple PDFs into a single one is a more complex operation than just concatenation. The API does not support PDF merge directly but it is easy to do it yourself on your end.

You can use a free utility called pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/). If you are on Linux, it is very likely that it is included as part of the distribution.

What you need to do is to save individual PDFs to temp files, use pdftk to merge them and echo the result. Here is untested code which should you give you the basic idea:

/* loop over urls and generate pdfs */
$pdfs = array();
for($i=0; $i<count($urls); $i+=1) {
    $pdf = $client->convertURI($urls[$i]);
    /* save the pdf to a temp file */
    $pdf_path = "/tmp/".$unique_orderid."_".$i.".pdf";
    $fp = fopen($pdf_path, "wb");
    fwrite($fp, $pdf);
    fclose($fp);
    /* remember the temp file */
    $pdfs[] = $pdf_path;
}

/* output HTTP headers */
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"".$title.".pdf\"");

/* merge */
$pdf_files = join(" ", $pdfs);
passthru("pdftk ".$pdf_files." cat output -");

/* delete the temp files */
foreach($pdfs as $fpath){
    unlink($fpath);
}
rarunkumar454@gmail.com wrote on 2019-08-01:
Hi,
I'm in need of converting multiple URLs into single PDF file. I need to perform this using .net core3.0 Application. Is there any possible ways for getting the result.
Thanks.
support wrote on 2019-08-02:
Hi,

Yes, it is possible but it is a multi-step procedure. First, you need to convert each web page to a separate PDF with our HTML to PDF API. You can then join the PDFs into a single one with our PDF to PDF API.

You can learn more in our documentation with examples for .NET (our API client library supports .NET core as well):

HTML to PDF
-- https://pdfcrowd.com/api/html-to-pdf-dotnet/

PDF to PDF
-- https://pdfcrowd.com/api/pdf-to-pdf-dotnet/