HiQPdf Next can convert multiple HTML documents into a single PDF using the HiQPdf.NextPdfMerge class. Each HTML document is converted to PDF, and the result is added to PdfMerge using the PdfMergeAddPdf(Byte, String) method. The PdfMerge class allows you to set security options, configure PDF viewer preferences and apply a digital signature to the resulting PDF.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class MergeMultipleHtmlToPdfController : Controller
{
public IActionResult Index()
{
var model = new MergeMultipleHtmlToPdfViewModel();
return View(model);
}
[HttpPost]
public ActionResult MergePdf(MergeMultipleHtmlToPdfViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Replace the demo serial number with the serial number received upon purchase
// to run the converter in licensed mode
Licensing.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// Create PDF merger
using PdfMerge pdfMerge = new PdfMerge();
// Set PDF merge options
SetPdfMergeOptions(pdfMerge, model);
// Create the list of URLs to convert
List<string> urlsToConvert = new List<string>() {
model.Url1,
model.Url2
};
foreach (string url in urlsToConvert)
{
// Create the HTML to PDF Converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// Set the HTML to PDF Converter options
SetHtmlToPdfConverterOptions(htmlToPdfConverter, model);
if (model.GenerateToc)
{
// adjust second TOC page numbers with the number of PDF pages generated by previous conversions
htmlToPdfConverter.Document.TableOfContents.PageNumbersOffset = pdfMerge.PdfMergeInfo.TotalPagesProduced;
}
// Convert HTML to PDF
byte[] pdfBytes = htmlToPdfConverter.ConvertUrlToMemory(url);
// Add the PDF to the merger
int firstPdfPageCount = pdfMerge.AddPdf(pdfBytes);
}
// Merge the PDF documents
byte[] mergedPdf = pdfMerge.Save();
// Send the merged PDF file to browser
FileResult fileResult = new FileContentResult(mergedPdf, "application/pdf");
fileResult.FileDownloadName = "Merge_Multipe_HTML.pdf";
return fileResult;
}
private void SetHtmlToPdfConverterOptions(HtmlToPdf htmlToPdfConverter, MergeMultipleHtmlToPdfViewModel model)
{
// set browser width
htmlToPdfConverter.BrowserWidth = model.BrowserWidth;
// set browser height if specified, otherwise use the default
if (model.BrowserHeight.HasValue)
htmlToPdfConverter.BrowserHeight = model.BrowserHeight.Value;
// set browser zoom
htmlToPdfConverter.BrowserZoom = model.BrowserZoom;
// Set an additional delay, in seconds, to wait for asynchronous content after the initial load
// The default value is 0
htmlToPdfConverter.WaitBeforeConvert = model.WaitTime;
// Set the maximum time, in seconds, to wait for the HTML page to load
// The default value is 120 seconds
htmlToPdfConverter.HtmlLoadedTimeout = model.LoadHtmlTimeout;
// Automatically resize the PDF page width to match the BrowserWidth property
// The default value is true
htmlToPdfConverter.Document.AutoResizePdfPageWidth = model.AutoResizePdfPageWidth;
// Set the PDF page size, which can be a predefined size like A4 or a custom size in points
// The default is A4
// Important Note: The PDF page width is automatically determined from the BrowserWidth
// when the AutoResizePdfPageWidth property is true
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize(model.PageSize);
// Set the PDF page orientation to Portrait or Landscape. The default is Portrait
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation(model.PageOrientation);
// Set the PDF page margins in points. The default is 0
htmlToPdfConverter.Document.Margins = new PdfMargins(
model.LeftMargin, model.RightMargin,
model.TopMargin, model.BottomMargin);
// Enable the creation of a hierarchy of bookmarks from H1 to H6 tags
htmlToPdfConverter.Document.GenerateDocumentOutline = model.AutoBookmarks;
// Enable or disable the automatic creation of a table of contents in the PDF document based on H1 to H6 HTML tags
htmlToPdfConverter.Document.GenerateTableOfContents = model.GenerateToc;
}
private void SetPdfMergeOptions(PdfMerge pdfMerge, MergeMultipleHtmlToPdfViewModel model)
{
// set the document security
pdfMerge.Security.OpenPassword = model.OpenPassword;
pdfMerge.Security.AllowPrinting = model.AllowPrinting;
}
private PdfPageSize GetSelectedPageSize(string pageSize)
{
switch (pageSize)
{
case "A0":
return PdfPageSize.A0;
case "A1":
return PdfPageSize.A1;
case "A10":
return PdfPageSize.A10;
case "A2":
return PdfPageSize.A2;
case "A3":
return PdfPageSize.A3;
case "A4":
return PdfPageSize.A4;
case "A5":
return PdfPageSize.A5;
case "A6":
return PdfPageSize.A6;
case "A7":
return PdfPageSize.A7;
case "A8":
return PdfPageSize.A8;
case "A9":
return PdfPageSize.A9;
case "ArchA":
return PdfPageSize.ArchA;
case "ArchB":
return PdfPageSize.ArchB;
case "ArchC":
return PdfPageSize.ArchC;
case "ArchD":
return PdfPageSize.ArchD;
case "ArchE":
return PdfPageSize.ArchE;
case "B0":
return PdfPageSize.B0;
case "B1":
return PdfPageSize.B1;
case "B2":
return PdfPageSize.B2;
case "B3":
return PdfPageSize.B3;
case "B4":
return PdfPageSize.B4;
case "B5":
return PdfPageSize.B5;
case "Flsa":
return PdfPageSize.Flsa;
case "HalfLetter":
return PdfPageSize.HalfLetter;
case "Ledger":
return PdfPageSize.Ledger;
case "Legal":
return PdfPageSize.Legal;
case "Letter":
return PdfPageSize.Letter;
case "Letter11x17":
return PdfPageSize.Letter11x17;
case "Note":
return PdfPageSize.Note;
default:
return PdfPageSize.A4;
}
}
private PdfPageOrientation GetSelectedPageOrientation(string pageOrientation)
{
return pageOrientation == "Portrait" ?
PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
}
}