'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Html

'' This sample shows how to render a hard-coded HTML string.
'' Adding GcHtml to your projects:
'' - Public classes and extension methods that enable rendering HTML
''   are provided by the GrapeCity.Documents.Html (GcHtml) package.
'' - GcHtml supports Windows, macOS And Linux platforms.
'' - Internally, it uses one of 3 system-dependent HTML engine packages:
''   -- GrapeCity.Documents.Html.Windows.X64
''   -- GrapeCity.Documents.Html.Mac.X64
''   -- GrapeCity.Documents.Html.Linux.X64
'' - GcHtml will automatically select the correct system-dependent engine package
''   at runtime, but that package must be referenced by your project so that GcHtml
''   can find it. You can add references to all 3 platform packages to your project,
''   Or if you only target one Or two platforms, you can add just the packages for
''   these target platforms.
'' Dealing with errors when using GcHtml:
'' - If GcHtml Is Not doing what you expect (e.g. rendering HTML to PDF
''   produces invalid PDFs) - check the content of the static string property
''   GcHtmlRenderer.LastLogMessage after the call to GcHtml returns,
''   it may explain why the error occurred (e.g. a required shared library
''   might be missing on Linux).
'' - Sometimes the HTML rendering process can hang without any diagnostics
''   being written to GcHtmlRenderer.LastLogMessage. GcHtml automatically kills
''   the hanging process in this case, but there Is no diagnostics.
''   Normally such situations should Not occur.
'' The above notes apply to any project that uses GcHtml.
Public Class HelloWorldHtml
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' HTML code that represents the content to render:
        Dim html = "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<style>" +
                "span.bold {" +
                    "font-weight: bold;" +
                "}" +
                "p.round {" +
                    "font: 36px arial, sans-serif;" +
                    "color: DarkSlateBlue;" +
                    "border: 4px solid SlateBlue;" +
                    "border-radius: 16px;" +
                    "padding: 3px 5px 3px 5px;" +
                    "text-shadow: 3px 2px LightSkyBlue;" +
                "}" +
                "</style>" +
                "</head>" +
                "<body>" +
                "<p class='round'>Hello, World, from <span class='bold'>GcHtml</span>!</p>" +
                "</body>" +

        '' Create a New PDF document, add a page, get graphics to draw on:
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

            '' Render HTML.
            '' The return value from DrawHtml() indicates whether anything has been rendered.
            '' The output parameter 'size' returns the actual size of the rendered content.
            Dim size As SizeF
            Dim ok = g.DrawHtml(html, 72, 72, New HtmlToPdfFormat(False) With {.MaxPageWidth = 6.5F}, size)

            '' If anything has been rendered, draw an extra border around the rendered content
            If ok Then
                Dim rc = New RectangleF(72 - 4, 72 - 4, size.Width + 8, size.Height + 8)
                g.DrawRoundRect(rc, 8, Color.PaleVioletRed)
            End If

            '' Optional diagnostics that may be useful to diagnose Chromium behavior
            '' (do Not leave such code in production, as the last log message might
            '' contain harmless messages even if all is ok)
            '' If (Not String.IsNullOrEmpty(GcHtmlRenderer.LastLogMessage)) Then
            ''   Util.AddNote(GcHtmlRenderer.LastLogMessage, page,
            ''     New RectangleF(72, 72 + size.Height + 36, page.Size.Width - 144, page.Size.Height - size.Height - 108))
            '' End If
        Catch ex As Exception
            Throw New Exception($"Error:\n{ex.Message}\nChromium last log message:\n{GcHtmlRenderer.LastLogMessage}")
        End Try

        '' Save the PDF
        Return doc.Pages.Count
    End Function
End Class