HelloWorldHtml.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Html;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This sample shows how to render an HTML string.
  15. //
  16. // Adding DsHtml to your projects:
  17. // - Public classes and extension methods that enable rendering HTML
  18. // are provided by the GrapeCity.Documents.Html (DsHtml) package.
  19. // - DsHtml supports Windows, macOS and Linux platforms.
  20. //
  21. // Dealing with errors when using DsHtml:
  22. // - If DsHtml is not doing what you expect (e.g. rendering HTML to PDF
  23. // produces an invalid PDF), check the content of the string property
  24. // GcHtmlBrowser.ErrorLog after the call to SaveAsPdf returns,
  25. // it may explain why the error occurred.
  26. //
  27. // DsHtml uses a local copy of a Chromium-based browser.
  28. // You can either rely on Google Chrome or Microsoft Edge browsers
  29. // installed in the operating system, or download an instance of
  30. // the Chromium browser to the application's local folder or some shared folder.
  31. // If you know the custom path to an installed Chromium-based browser,
  32. // use it instead of the above-mentioned options.
  33. // The BrowserFetcher class can be used to install or fetch an already
  34. // installed browser, you can then use the BrowserFetcher.GetDownloadedPath()
  35. // to get the path to the browser. See the BrowserFetcher class for methods
  36. // retrieving the path to Chrome or Edge executables and for other
  37. // options and methods that help to download Chromium browser
  38. // to a local folder if necessary.
  39. //
  40. // See the Util.NewHtmlBrowser() utility method used by this sample for a
  41. // implementation example.
  42. //
  43. // The above notes apply to any project that uses DsHtml.
  44. public class HelloWorldHtml
  45. {
  46. public int CreatePDF(Stream stream)
  47. {
  48. // HTML code that represents the content to render:
  49. var html = "<!DOCTYPE html>" +
  50. "<html>" +
  51. "<head>" +
  52. "<style>" +
  53. "span.bold {" +
  54. " font-weight: bold;" +
  55. "}" +
  56. "p.round {" +
  57. " font: 36px arial, sans-serif;" +
  58. " color: DarkSlateBlue;" +
  59. " border: 4px solid SlateBlue;" +
  60. " border-radius: 16px;" +
  61. " padding: 3px 5px 3px 5px;" +
  62. " text-shadow: 3px 2px LightSkyBlue;" +
  63. "}" +
  64. "p.note {" +
  65. " font: 16px arial, sans-serif;" +
  66. " color: DarkSlateBlue;" +
  67. "}" +
  68. "</style>" +
  69. "</head>" +
  70. "<body>" +
  71. "<p class='round'>Hello, World, from <span class='bold'>DsHtml</span>!</p>" +
  72. "<p class='note'>This demo shows how to use <strong>DsHtml</strong> to render HTML to PDF or images." +
  73. "Check out the source code of this demo for details on how to configure and use <strong>DsHtml</strong>.</p>" +
  74. "</body>";
  75.  
  76. // Create a new PDF document, add a page, get graphics to draw on:
  77. var doc = new GcPdfDocument();
  78. var page = doc.NewPage();
  79. var g = page.Graphics;
  80.  
  81. try
  82. {
  83. // Create an instance of GcHtmlBrowser that is used to render HTML:
  84. using var browser = Common.Util.NewHtmlBrowser();
  85.  
  86. // Render HTML.
  87. // The return value from DrawHtml() indicates whether anything has been rendered.
  88. // The output parameter 'size' returns the actual size of the rendered content.
  89. var ok = g.DrawHtml(browser, html, 72, 72,
  90. new HtmlToPdfFormat(false) { MaxPageWidth = 6.5f, MaxPageHeight = 9f },
  91. out SizeF size);
  92.  
  93. if (ok)
  94. {
  95. // If anything has been rendered, draw an extra border around the rendered content:
  96. var rc = new RectangleF(72 - 4, 72 - 4, size.Width + 8, size.Height + 8);
  97. g.DrawRoundRect(rc, 8, Color.PaleVioletRed);
  98. }
  99. else if (!string.IsNullOrEmpty(browser.ErrorLog))
  100. {
  101. // Optional diagnostics that may be useful when diagnosing browser errors.
  102. // Note that the error log may contain harmless information messages
  103. // even if there were no errors, so testing whether the error log is empty
  104. // should not be used as the error indicator.
  105. Common.Util.AddNote(browser.ErrorLog, page,
  106. new RectangleF(72, 72 + size.Height + 36, page.Size.Width - 144, page.Size.Height - size.Height - 108));
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. throw new Exception($"Error:\n{ex.Message}");
  112. }
  113.  
  114. // Done:
  115. doc.Save(stream);
  116. return doc.Pages.Count;
  117. }
  118. }
  119. }
  120.