1: ''' <summary>
2: ''' Convert the specified HTML string to a PDF document and send the
3: ''' document as an attachment to the browser
4: ''' </summary>
5: ''' <remarks></remarks>
6: Private Sub ConvertHTMLStringToPDF()
7:
8: Dim htmlString As String = textBoxHTMLCode.Text
9: Dim baseURL As String = textBoxBaseURL.Text.Trim()
10: Dim selectablePDF As Boolean = radioConvertToSelectablePDF.Checked
11:
12: ' Create the PDF converter. Optionally you can specify the virtual browser
13: ' width as parameter. 1024 pixels is default, 0 means autodetect
14: Dim pdfConverter As PdfConverter = New PdfConverter()
15: ' set the license key
14: pdfConverter.LicenseKey =
"P38cBx6AWW7b9c81TjEGxnrazP+J7rOjs+9omJ3TUycauK+cLWdrITM5T59hdW5r";
17: ' set the converter options
18: pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4
19: pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal
20: pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait
21: pdfConverter.PdfDocumentOptions.ShowHeader = False
22: pdfConverter.PdfDocumentOptions.ShowFooter = False
23: ' set to generate selectable pdf or a pdf with embedded image
24: pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = selectablePDF
25:
26: ' Performs the conversion and get the pdf document bytes that you can further
27: ' save to a file or send as a browser response
28: '
29: ' The baseURL parameter helps the converter to get the CSS files and images
30: ' referenced by a relative URL in the HTML string. This option has efect only
' if the HTML string contains a valid HEAD tag.
31: ' The converter will automatically inserts a <BASE HREF="baseURL"> tag.
32: Dim pdfBytes As Byte() = Nothing
33: If (baseURL.Length > 0) Then
34: pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(htmlString, baseURL)
35: Else
36: pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(htmlString)
37: End If
38:
39: ' send the PDF document as a response to the browser for download
40: Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
41: response.Clear()
42: response.AddHeader("Content-Type", "binary/octet-stream")
43: response.AddHeader(
"Content-Disposition", "attachment; filename=ConversionResult.pdf;
size=" & pdfBytes.Length.ToString())
44: response.Flush()
45: response.BinaryWrite(pdfBytes)
46: response.Flush()
47: response.End()
48: End Sub