Render a Web page to PDF specifying page orientation, margins, headers and other optons

PDF TIFF SVG JPG C# VB
HtmlSettings.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 web page specified by a URL
  12. '' to a PDF. Here we render the C# source of the WordIndex sample.
  13. '' Similar to HtmlRenderPage0, this sample directly uses GcHtmlRenderer,
  14. '' but shows how to specify options such as page orientation, margins,
  15. '' headers and footers.
  16. ''
  17. '' For reference, the following markup extensions can be used in the
  18. '' header and title templates (all except title are used in this sample):
  19. '' - <span class="date"> - Formatted date
  20. '' - <span class="title"> - Document title
  21. '' - <span class="url"> - Document location
  22. '' - <span class="pageNumber"> - Current page number
  23. '' - <span class="totalPages"> - Total number of pages
  24. ''
  25. '' Note that in headers/footers images cannot be specified as links,
  26. '' but base64 encoded data is supported as shown in this sample.
  27. ''
  28. '' Also note that for background colors in headers/footers to appear,
  29. '' the following webkit CSS extension should be specified:
  30. '' -webkit-print-color-adjust: exact;
  31. ''
  32. '' Please see notes in comments at the top of HelloWorldHtml
  33. '' sample code for details on adding DsHtml to your projects.
  34. Public Class HtmlSettings
  35. Sub CreatePDF(ByVal stream As Stream)
  36. '' Get a temporary file where the web page will be rendered:
  37. Dim tmp = Path.GetTempFileName()
  38. '' The Uri of the web page to render:
  39. Dim uri = New Uri("https://developer.mescius.com/document-solutions/dot-net-pdf-api/demos/view-source-cs/word-index/")
  40. '' Image used in the footer template:
  41. Dim image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAF8WlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWh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Lub3AXMSla1NjRZtwFLgEcZ1rZ7Yv5bMWfgtxHWwpaSLz0xTLEBXlVuf6YiHJhEcQdar6nCMGOFYiSlTlfTe60GxMN3Adok7VBwXKshLbEPW8rkRUw8wbxUpnfQ6cw8k6VQMVbYh6XtMRJBaMYucDVwGD6KPGgI1YgSi/9+diCjUroXofouTcKybJzwWrEQ7iLjOEm5mRbgGuAGYCn5moJxO+RBS5vQQRopgCK1L6HyKqWF5DgRN2luhGBMHjgC1mK7NyT+QFwI+oU2UWHuvV8ZSJOhJg9aaShqiCNAxYV0S5G4FRiGrnud0JKxB2VZk8CFyEqBBSV4CcBkTFtbkFyskbdpfpfBdhOUvIzXI0RM2/GuBtE/qVNewm8DieQCypnsii7TOAD/i1if3JGgOFQIAwwhLHAU8isj89vf/qEMRNAm5AVJQbEPgfbZlv3OD+pJUAAAAASUVORK5CYII="
  42.  
  43. '' Create an instance of GcHtmlBrowser that is used to render HTML:
  44. Using browser = Util.NewHtmlBrowser()
  45. '' PdfOptions specifies options for HTML to PDF conversion:
  46. '' - PageRanges specifies which pages to include.
  47. '' - PageWidth/PageHeight allow customizing page size (defaults are used here for demo).
  48. '' - Margins specify page margins (the default is no margins).
  49. '' - Landscape allows changing page orientation.
  50. '' - Scale allows enlarging or reducing the render size (default is 1).
  51. '' - To add custom headers, DisplayHeaderFooter needs to be set to true.
  52. '' - HeaderTemplate/FooterTemplate allow you to specify custom page headers.
  53. Dim pdfOptions = New PdfOptions() With
  54. {
  55. .PageRanges = "2-100",
  56. .PageWidth = 8.5F,
  57. .PageHeight = 11.0F,
  58. .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
  59. .Landscape = True,
  60. .DisplayHeaderFooter = True,
  61. .HeaderTemplate = "<div style='-webkit-print-color-adjust:exact;background-color:#395daa;color:white;" +
  62. "padding:0.1in;font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
  63. "<span style='float:left'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
  64. "<span style='float:right'>Document created on <span class='date'></span>" +
  65. "</div>",
  66. .FooterTemplate = "<div style='font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
  67. $"<span>Document location: <span class='url'></span><img style='float:right;' width='40' height='40' src='{image}'></img></div>"
  68. }
  69. '' Render the source Web page to the temporary file:
  70. Using htmlPage = browser.NewPage(uri)
  71. htmlPage.SaveAsPdf(tmp, pdfOptions)
  72. End Using
  73. End Using
  74. '' Copy the created PDF from the temp file to target stream:
  75. Using ts = File.OpenRead(tmp)
  76. ts.CopyTo(stream)
  77. End Using
  78. '' Clean up:
  79. File.Delete(tmp)
  80. '' Done.
  81. End Sub
  82. End Class
  83.