You can get the most accurate HTML to PDF conversion with
HiQPdf HTML to PDF Converter for .NET offers the. For small tasks you can use the
free HTML to PDF .NET library in your applications. The free version doesn't offer all the features of the full library but it is enough to create small PDFs. The C# sample code for converting HTML to PDF in ASP.NET is:
Code:
protected void buttonConvertToPdf_Click(object sender, EventArgs e)
{
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set browser width
htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
// set browser height if specified, otherwise use the default
if (textBoxBrowserHeight.Text.Length > 0)
htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);
// set HTML Load timeout
htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);
// set PDF page size and orientation
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();
// set the PDF standard used by the document
htmlToPdfConverter.Document.PdfStandard = checkBoxPdfA.Checked ? PdfStandard.PdfA : PdfStandard.Pdf;
// set PDF page margins
htmlToPdfConverter.Document.Margins = new PdfMargins(5);
// set whether to embed the true type font in PDF
htmlToPdfConverter.Document.FontEmbedding = checkBoxFontEmbedding.Checked;
// set triggering mode; for WaitTime mode set the wait time before convert
switch (dropDownListTriggeringMode.SelectedValue)
{
case "Auto":
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
case "WaitTime":
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime;
htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
break;
case "Manual":
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Manual;
break;
default:
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
}
// set header and footer
SetHeader(htmlToPdfConverter.Document);
SetFooter(htmlToPdfConverter.Document);
// set the document security
htmlToPdfConverter.Document.Security.OpenPassword = textBoxOpenPassword.Text;
htmlToPdfConverter.Document.Security.AllowPrinting = checkBoxAllowPrinting.Checked;
// set the permissions password too if an open password was set
if (htmlToPdfConverter.Document.Security.OpenPassword != null && htmlToPdfConverter.Document.Security.OpenPassword != String.Empty)
htmlToPdfConverter.Document.Security.PermissionsPassword = htmlToPdfConverter.Document.Security.OpenPassword + "_admin";
// convert HTML to PDF
byte[] pdfBuffer = null;
if (radioButtonConvertUrl.Checked)
{
// convert URL to a PDF memory buffer
string url = textBoxUrl.Text;
pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
}
else
{
// convert HTML code
string htmlCode = textBoxHtmlCode.Text;
string baseUrl = textBoxBaseUrl.Text;
// convert HTML code to a PDF memory buffer
pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
}
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document, attachment or inline, and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=HtmlToPdf.pdf; size={1}",
checkBoxOpenInline.Checked ? "inline" : "attachment", pdfBuffer.Length.ToString()));
// write the PDF buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
}