The HTML to SVG converter allows you to quickly create a vectorial representation of the HTML documents in SVG format. The class representing the HTML to SVG converter is HiQPdfClientHtmlToSvg.
The most important features of the HTML to SVG Converter are described in detail in the topics below.
The SerialNumber property of the of the HiQPdfClient.HtmlToSvg class must be set with the purchased serial number as described in the License Purchasing section. In the sample code below is set with an evaluation serial number.
The HtmlToSvgDPI property can be used to control the SVG document resolution in DPI.
The SetSize property controls if the calculated size of the document is set in the SVG document.
The HtmlLoadedTimeout property of the HiQPdfClient.HtmlToSvg class controls the maximum time in seconds to wait for HTML document to be loaded. The default value is 120 seconds. An exception is thrown if the HTML document cannot be loaded in HtmlLoadedTimeout seconds.
The Convert methods of the of the HiQPdfClient.HtmlToSvg class are finally called to convert HTML documents to SVG. Function of how the HTML document to be converted is given, by URL or by content, and function of where the output SVG document is written the following Convert methods are defined in the HiQPdfClient.HtmlToSvg class:
Methods to convert the HTML document from a given URL to SVG:
HtmlToSvgConvertUrlToFile(String, String) - Converts a HTML document from a given URL to a SVG file
HtmlToSvgConvertUrlToMemory(String) - Converts a HTML document from a given URL and produces a SVG document as a memory buffer
Methods to convert a HTML code to SVG:
HtmlToSvgConvertHtmlToFile(String, String, String) - Converts a given HTML code to a SVG file
HtmlToSvgConvertHtmlToMemory(String, String) - Converts a given HTML code and produces a SVG document as a memory buffer
In this demo you can convert an URL, a local file or a custom HTML code to a SVG vectorial image. You can set the browser width in pixels. In evaluation mode only the top part of the HTML document is converted to SVG. In the licensed version there is not such a limitation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdfClient;
namespace HiQPdf_Demo.Controllers
{
public class ConvertHtmlToSvgController : Controller
{
// GET: ConvertHtmlToSvg
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToSvg(IFormCollection collection)
{
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// create the HTML to SVG converter
HtmlToSvg htmlToSvgConverter = new HtmlToSvg(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
htmlToSvgConverter.ServerPassword = serverPassword;
// set a demo serial number
htmlToSvgConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set browser width
htmlToSvgConverter.BrowserWidth = int.Parse(collection["textBoxBrowserWidth"]);
// set browser height if specified, otherwise use the default
if (collection["textBoxBrowserHeight"][0].Length > 0)
htmlToSvgConverter.BrowserHeight = int.Parse(collection["textBoxBrowserHeight"]);
// set HTML Load timeout
htmlToSvgConverter.HtmlLoadedTimeout = int.Parse(collection["textBoxLoadHtmlTimeout"]);
// set triggering mode; for WaitTime mode set the wait time before convert
switch (collection["dropDownListTriggeringMode"])
{
case "Auto":
htmlToSvgConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
case "WaitTime":
htmlToSvgConverter.TriggerMode = ConversionTriggerMode.WaitTime;
htmlToSvgConverter.WaitBeforeConvert = int.Parse(collection["textBoxWaitTime"]);
break;
case "Manual":
htmlToSvgConverter.TriggerMode = ConversionTriggerMode.Manual;
break;
default:
htmlToSvgConverter.TriggerMode = ConversionTriggerMode.Auto;
break;
}
// convert to SVG
string svgFileName = "HtmlToSvg.svg";
byte[] svgBuffer = null;
if (collection["UrlOrHtmlCode"] == "radioButtonConvertUrl")
{
// convert URL
string url = collection["textBoxUrl"];
svgBuffer = htmlToSvgConverter.ConvertUrlToMemory(url);
}
else
{
// convert HTML code
string htmlCode = collection["textBoxHtmlCode"];
string baseUrl = collection["textBoxBaseUrl"];
svgBuffer = htmlToSvgConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
}
FileResult fileResult = new FileContentResult(svgBuffer, "image/svg+xml");
fileResult.FileDownloadName = svgFileName;
return fileResult;
}
}
}