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 a hard-coded 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. "</style>" +
  60. "</head>" +
  61. "<body>" +
  62. "<p class='round'>Hello, World, from <span class='bold'>DsHtml</span>!</p>" +
  63. "</body>" +
  64. "</html>"
  65.  
  66. '' Create a New PDF document, add a page, get graphics to draw on:
  67. Dim doc = New GcPdfDocument()
  68. Dim page = doc.NewPage()
  69. Dim g = page.Graphics
  70.  
  71. Try
  72. '' Create an instance of GcHtmlBrowser that is used to render HTML:
  73. Using browser = Util.NewHtmlBrowser()
  74.  
  75. '' Render HTML.
  76. '' The return value from DrawHtml() indicates whether anything has been rendered.
  77. '' The output parameter 'size' returns the actual size of the rendered content.
  78. Dim size As SizeF
  79. Dim ok = g.DrawHtml(browser, html, 72, 72, New HtmlToPdfFormat(False) With {.MaxPageWidth = 6.5F}, size)
  80.  
  81. '' If anything has been rendered, draw an extra border around the rendered content
  82. If ok Then
  83. Dim rc = New RectangleF(72 - 4, 72 - 4, size.Width + 8, size.Height + 8)
  84. g.DrawRoundRect(rc, 8, Color.PaleVioletRed)
  85. ElseIf Not String.IsNullOrEmpty(browser.ErrorLog) Then
  86. '' Optional diagnostics that may be useful when diagnosing browser errors.
  87. '' Note that the error log may contain harmless information messages
  88. '' even if there were no errors, so testing whether the error log is empty
  89. '' should not be used as the error indicator.
  90. Util.AddNote(browser.ErrorLog, page,
  91. New RectangleF(72, 72 + size.Height + 36, page.Size.Width - 144, page.Size.Height - size.Height - 108))
  92. End If
  93. End Using
  94. Catch ex As Exception
  95. Throw New Exception($"Error:\n{ex.Message}")
  96. End Try
  97.  
  98. '' Save the PDF
  99. doc.Save(stream)
  100. Return doc.Pages.Count
  101. End Function
  102. End Class
  103.