Articles in this section
Category / Section

How to convert multiple HTML documents to a single PDF?

2 mins read

You can convert multiple HTML documents into a single PDF document by converting each HTML as a separate PDF and then merging them together. Sample for this workaround is provided as follows.

C#

// Create PDF Generator
PdfDocument doc = new PdfDocument();
// Add new Page
PdfPage page = doc.Pages.Add();
PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
float height = -1;
using (HtmlConverter html = new HtmlConverter())
{
//Give the link where the Chart present
HtmlToPdfResult result = html.Convert("www.google.com", ImageType.Metafile, (int)width, (int)height, AspectRatio.FitPageSize);
if (result != null)
{
PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
doc.PageSettings.Height = result.RenderedImage.Size.Height;
format.SplitTextLines = false;
format.SplitImages = false;
result.Render(page, format);
}
}
// Save PDF document
doc.Save(doc1Stream);
doc.Close(true);
doc = new PdfDocument();
page = doc.Pages.Add();
MemoryStream doc2Stream = new MemoryStream();
using (HtmlConverter html = new HtmlConverter())
{
HtmlToPdfResult result = html.Convert(htmlString, "", ImageType.Metafile, (int)width, (int)height, AspectRatio.FitPageSize);
if (result != null)
{
PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
doc.PageSettings.Height = result.RenderedImage.Size.Height;
format.SplitTextLines = false;
format.SplitImages = false;
result.Render(page, format);
}
}
doc.Save(doc2Stream);
doc.Close(true);
//Importing 2nd PDF to 1st PDF
PdfLoadedDocument srcDoc1 = new PdfLoadedDocument(doc1Stream);
PdfLoadedDocument srcDoc2 = new PdfLoadedDocument(doc2Stream);
srcDoc1.ImportPageRange(srcDoc2, 0, srcDoc2.Pages.Count - 1);
//Save the Merged PDF
srcDoc1.Save("Sample.pdf");

Sample link

https://www.syncfusion.com/downloads/support/directtrac/general/MultipleHtmlToPdf1484926822.zip


Conclusion:

I hope you enjoyed learning about how to convert multiple HTML documents to a single PDF.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied