Articles in this section
Category / Section

How to convert multiple HTML documents to a single PDF?

1 min 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

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