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