The converter offers two conversion triggering modes: Auto and Manual. The triggering mode is controlled by the HtmlToPdfTriggerMode property of the converter.
When the triggering mode is Auto, the conversion to PDF starts immediately after the HTML document is loaded in the converter and this default behavior is suitable for converting most HTML documents. Optionally, you can specify an additional time, in seconds, to wait before starting the conversion, allowing more time for JavaScript to update the web page, using the WaitBeforeConvert property of the converter.
For more precise control over when the conversion starts, you can use the Manual triggering mode. In this case, the converter waits for the hiqPdfConverter_startConversion() method to be manually called from JavaScript. This method is available only when the conversion triggering mode is set to Manual.
Below is an example of an HTML document that requires manual triggering of the conversion. In the provided sample script, a ticks counter is incremented every 30 ms after the document is loaded. When the ticks count reaches 100, after approximately 3 seconds, the hiqPdfConverter_startConversion() method is called if it is available.
In the HTML script below, you can also notice the usage of the hiqPdfInfo.Version member. This member is exposed by the converter to the JavaScript code in the HTML document being converted to provide information about the HiQPdf library version. The existence of the hiqPdfInfo object in JavaScript can be used to determine whether the document is currently loaded in the HiQPdf converter.
<html>
<head>
<title>Conversion Triggering Mode</title>
</head>
<body>
<br />
<br />
<span style="font-family: Times New Roman; font-size: 10pt">
When the triggering mode is 'Manual' the conversion is triggered by the call to <b>hiqPdfConverter_startConversion()</b>
from JavaScript.<br />
In this example document the hiqPdfConverter_startConversion() method is called when the ticks count
reached 100 which happens in about 3 seconds.
</span>
<br />
<br />
<b>Ticks Count:</b> <span style="color: Red" id="ticks">0</span>
<br />
<br />
<!-- display HiQPdf HTML converter version if the document is loaded in converter-->
<span style="font-family: Times New Roman; font-size: 10pt">
HiQPdf Info:
<script type="text/javascript">
// check if the document is loaded in HiQPdf HTML to PDF Converter
if (typeof hiqPdfInfo == "undefined") {
// hiqPdfInfo object is not defined and the document is loaded in a browser
document.write("Not in HiQPdf");
}
else {
// hiqPdfInfo object is defined and the document is loaded in converter
document.write(hiqPdfInfo.Version);
}
</script>
</span>
<br />
<script type="text/javascript">
var ticks = 0;
function tick() {
// increment ticks count
ticks++;
var ticksElement = document.getElementById("ticks");
// set ticks count
ticksElement.innerHTML = ticks;
if (typeof hiqPdfConverter_startConversion == 'function' && ticks == 100) {
// trigger conversion
ticksElement.style.color = "green";
hiqPdfConverter_startConversion();
}
else {
// wait one more tick
setTimeout("tick()", 30);
}
}
tick();
</script>
</body>
</html>In this demo, you learn how to configure the converter triggering mode. There are two conversion triggering modes: Auto and Manual. In the sample script above, a ticks counter is incremented every 30 ms after the document is loaded. When the ticks count reaches 100, after approximately 3 seconds, the hiqPdfConverter_startConversion() method is called if the conversion triggering mode is set to Manual and the function is available.
When the triggering mode is set to Manual and the ticks count reaches exactly 100, the hiqPdfConverter_startConversion() method is called to start the conversion to PDF.
When the triggering mode is Auto, the conversion starts immediately after the wait time expires.
using System.IO;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class ConversionTriggeringModeController : Controller
{
IWebHostEnvironment m_hostingEnvironment;
public ConversionTriggeringModeController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: ConversionTriggeringMode
public ActionResult Index()
{
var model = SetViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertToPdf(ConversionTriggeringModeViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.SerialNumber = "your-serial-number";
// Create an HTML to PDF converter object with default settings
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set triggering mode; for WaitTime mode set the wait time before convert
switch (model.TriggeringMode)
{
case "Auto":
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
htmlToPdfConverter.WaitBeforeConvert = model.WaitTime;
break;
case "Manual":
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Manual;
break;
default:
htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
}
// convert the HTML to PDF
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(model.HtmlCode, null);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "TriggeringMode.pdf";
return fileResult;
}
private ConversionTriggeringModeViewModel SetViewModel()
{
var model = new ConversionTriggeringModeViewModel();
var contentRootPath = Path.Combine(m_hostingEnvironment.ContentRootPath, "wwwroot");
model.HtmlCode = System.IO.File.ReadAllText(Path.Combine(contentRootPath, "DemoFiles/Html/TriggeringMode.html"));
return model;
}
}
}