With EVO HTML to PDF Converter you can also convert HTML to raster images or HTML to SVG.
HTML to Image Conversion
There two approaches to convert HTML to Image. The first is to use the ImgConverter class. The second approach is to create a Document object and to add a HtmlToImageElement to it. Below are described in detail both methods.
Convert HTML to Image using the ImgConverter Class
The easiest approach is to use one of the ImgConverter class methods to convert an URL or a HTML string to a raster image. The resulted image can be:
produced in a memory buffer using the ImgConverter..::..GetImageBytesFromUrl(String, ImageFormat) and ImgConverter..::..GetImageBytesFromHtmlString(String, ImageFormat, String) methods
saved in a file on disk using the ImgConverter..::..SaveImageFromUrlToFile(String, ImageFormat, String) and ImgConverter..::..SaveImageFromHtmlStringToFile(String, ImageFormat, String, String) methods
Code Sample - Convert URL to Image with ImgConverter Class
| C# | |
|---|---|
/// <summary> /// Convert the HTML code from the specified URL to a PNG image and send the /// image as an attachment to the browser /// </summary> private void ConvertURLToImage() { string urlToConvert = textBoxWebPageURL.Text.Trim(); // Create the Image converter. Optionally the HTML viewer width can be specified as parameter // The default HTML viewer width is 1024 pixels. ImgConverter imgConverter = new ImgConverter(); // set the license key imgConverter.LicenseKey = "B4mYiJubiJiInIaYiJuZhpmahpGRkZE="; // set if the JavaScript is enabled during conversion - default is true imgConverter.JavaScriptEnabled = cbClientScripts.Checked; // Performs the conversion and get the image bytes that can be further // saved to a file or sent as a response to browser byte[] imgBytes = imgConverter.GetImageBytesFromUrl(urlToConvert, System.Drawing.Imaging.ImageFormat.Png); // send the image as a response to the browser for download System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.AddHeader("Content-Type", "image/png"); response.AddHeader("Content-Disposition", String.Format("attachment; filename=GettingStarted.png; size={0}", imgBytes.Length.ToString())); response.BinaryWrite(imgBytes); // Note: it is important to end the response, otherwise the ASP.NET // web page will render its content to image stream response.End(); } | |
Another approach is to add HTML content as a raster image to a PDF document using HtmlToImageElement objects. The initial Document to which the HTML elements are added are either new documents created using one of the class constructor or documents initialized with the result of a HTML to PDF conversion using the PdfConverter..::..GetPdfDocumentObjectFromUrl(String) and PdfConverter..::..GetPdfDocumentObjectFromHtmlString(String, String) methods.
Code Sample - Convert URL to Image with HtmlToImageElement Class
| C# | |
|---|---|
//create a PDF document Document document = new Document(); // set the license key document.LicenseKey = "B4mYiJubiJiInIaYiJuZhpmahpGRkZE="; //optional settings for the PDF document like margins, compression level, //security options, viewer preferences, document information, etc document.CompressionLevel = PdfCompressionLevel.Normal; document.Margins = new Margins(10, 10, 0, 0); //document.Security.CanPrint = true; //document.Security.UserPassword = ""; document.ViewerPreferences.HideToolbar = false; // set if the images are compressed in PDF with JPEG to reduce the PDF document size document.JpegCompressionEnabled = cbJpegCompression.Checked; //Add a first page to the document. The next pages will inherit the settings from this page PdfPage page = document.Pages.AddNewPage(PdfPageSize.A4, new Margins(10, 10, 0, 0), PdfPageOrientation.Portrait); // the code below can be used to create a page with default settings A4, document margins inherited, portrait orientation //PdfPage page = document.Pages.AddNewPage(); // add a font to the document that can be used for the texts elements PdfFont font = document.Fonts.Add(new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point)); // add header and footer before renderng the content if (cbAddHeader.Checked) AddHtmlHeader(document); if (cbAddFooter.Checked) AddHtmlFooter(document, font); // the result of adding an element to a PDF page AddElementResult addResult; // Get the specified location and size of the rendered content // A negative value for width and height means to auto determine // The auto determined width is the available width in the PDF page // and the auto determined height is the height necessary to render all the content float xLocation = float.Parse(textBoxXLocation.Text.Trim()); float yLocation = float.Parse(textBoxYLocation.Text.Trim()); float width = float.Parse(textBoxWidth.Text.Trim()); float height = float.Parse(textBoxHeight.Text.Trim()); if (radioConvertToSelectablePDF.Checked) { // convert HTML to PDF HtmlToPdfElement htmlToPdfElement; if (radioConvertURL.Checked) { // convert a URL to PDF string urlToConvert = textBoxWebPageURL.Text.Trim(); htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height, urlToConvert); } else { // convert a HTML string to PDF string htmlStringToConvert = textBoxHTMLCode.Text; string baseURL = textBoxBaseURL.Text.Trim(); htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height, htmlStringToConvert, baseURL); } //optional settings for the HTML to PDF converter htmlToPdfElement.FitWidth = cbFitWidth.Checked; htmlToPdfElement.EmbedFonts = cbEmbedFonts.Checked; htmlToPdfElement.LiveUrlsEnabled = cbLiveLinks.Checked; htmlToPdfElement.JavaScriptEnabled = cbClientScripts.Checked; htmlToPdfElement.PdfBookmarkOptions.HtmlElementSelectors = cbBookmarks.Checked ? new string[] { "H1", "H2" } : null; // add theHTML to PDF converter element to page addResult = page.AddElement(htmlToPdfElement); } else { HtmlToImageElement htmlToImageElement; // convert HTML to image and add image to PDF document if (radioConvertURL.Checked) { // convert a URL to PDF string urlToConvert = textBoxWebPageURL.Text.Trim(); htmlToImageElement = new HtmlToImageElement(xLocation, yLocation, width, height, urlToConvert); } else { // convert a HTML string to PDF string htmlStringToConvert = textBoxHTMLCode.Text; string baseURL = textBoxBaseURL.Text.Trim(); htmlToImageElement = new HtmlToImageElement(xLocation, yLocation, width, height, htmlStringToConvert, baseURL); } //optional settings for the HTML to PDF converter htmlToImageElement.FitWidth = cbFitWidth.Checked; htmlToImageElement.LiveUrlsEnabled = cbLiveLinks.Checked; htmlToImageElement.JavaScriptEnabled = cbClientScripts.Checked; htmlToImageElement.PdfBookmarkOptions.HtmlElementSelectors = cbBookmarks.Checked ? new string[] { "H1", "H2" } : null; addResult = page.AddElement(htmlToImageElement); } if (cbAdditionalContent.Checked) { // The code below can be used add some other elements right under the conversion result // like texts or another HTML to PDF conversion // add a text element right under the HTML to PDF document PdfPage endPage = document.Pages[addResult.EndPageIndex]; TextElement nextTextElement = new TextElement(0, addResult.EndPageBounds.Bottom + 10, "Below there is another HTML to PDF Element", font); nextTextElement.ForeColor = System.Drawing.Color.Green; addResult = endPage.AddElement(nextTextElement); // add another HTML to PDF converter element right under the text element endPage = document.Pages[addResult.EndPageIndex]; HtmlToPdfElement nextHtmlToPdfElement = new HtmlToPdfElement(0, addResult.EndPageBounds.Bottom + 10, "http://www.google.com"); addResult = endPage.AddElement(nextHtmlToPdfElement); } try { // get the PDF document bytes byte[] pdfBytes = document.Save(); // send the generated PDF document to client browser // get the object representing the HTTP response to browser HttpResponse httpResponse = HttpContext.Current.Response; // add the Content-Type and Content-Disposition HTTP headers httpResponse.AddHeader("Content-Type", "application/pdf"); httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename=HtmlToPdfElement.pdf; size={0}", pdfBytes.Length.ToString())); // write the PDF document bytes as attachment to HTTP response httpResponse.BinaryWrite(pdfBytes); // Note: it is important to end the response, otherwise the ASP.NET // web page will render its content to PDF document stream httpResponse.End(); } finally { // close the PDF document to release the resources document.Close(); } } | |