Extensions: PDF File Action

Converting HTML to PDF

By Kevin M Schreiner

April 20, 2010

This package provides a new action which can convert HTML to PDF on the fly. The library works similarly to that of the File Action in that it takes incoming Source and outgoing Target parameters to provide its runtime instructions. A third party library from DuoDimension.com is used to handle the conversion.

The package has been provided with references to both Full and Trial version libraries of the pdfduo-net library, available for purchase via http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx. The library requires a LicenseKey for verification of appropriate usage. The Trial version and Full version differ by the call to Set the License Key in the runtime of the plugin.

An attempt to use the Full version DLL without the License Key will result in an error and no conversion will occur. 

The trial DLL, which matches the functionality, but without the call to set the key will place warning message in the output.

To test out the library and get it working within your environment, or setup your environment with the Full version after you are licensed, following the provided steps:

  1. Navigate to either the Runtime/Trial or Runtime/Full directory, depending on your license.
  2. Copy OpenWebStudio.PDFFile.config and place it in the root of the website.
  3. Copy r2i.OWS.Extensions.PDF.dll from the /bin folder and place it in /bin of the website.
  4. Obtain your copy of pdfduo-net.dll from http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx
    For trial, download the trial version.
    For full, purchase the full version.
  5. Copy the pdfduo-net.dll from step #4 into /bin of the website.
  6. Copy r2i.OWS.Extensions.PDF.js from the /DesktopModules/OWS/Scripts directory, placing it in /DesktopModules/OWS/Scripts of the website

Now, when you use the OpenWebStudio admin, a new Action will be present named PDFFile.

To use PDFFile, it operates the same as the File Action taking the following attributes:

License Key - needed for the Full Version to convert from HTML to PDF.

Source Type - identifies the source of the HTML, either Runtime Value or File Path

Variable Type - identifies the Type of the variable, if Source Type is set to Runtime Value

Source - the Name of the variable if Source Type is Runtime Value, or the Source Path of the file if it is File Path

Destination Type - the target of the PDF file. Options are Response, Runtime Value, File Path or Email Attachment.

Response Type - the Content (MIME) Type of the response, which should be application/pdf. This is used only if the target is Email or Response.

Destination - the File Name for Email or File Path, otherwise the name of the target value.

 

To utilize a different conversion library, simply extend the provided Source library. The functionality needed for setting up the

output and consume the provided attributes is handled through a combination of functions. To change the physical library of choice,

just change the Convert_to_PDF function itself.

Private Function Convert_to_PDF(ByVal LicenseKey As String, ByVal Source As IO.Stream) As Byte()

If Not LicenseKey Is Nothing AndAlso LicenseKey.Length > 0 Then

DuoDimension.HtmlToPdf.SetLicenseCode(LicenseKey)

End If

Dim conv As New DuoDimension.HtmlToPdf

conv.OpenHTML(Source)

Return conv.SavePDF()

End Function

Using the following code logic, you can utilize a PDF Conversion library which contains a bit more flexibility and capability. The converter, available from http://www.html-to-pdf.net is utilized instead in the references in code (download the dll and modify the provided source code). In the existing source code, replace the Convert_to_PDF function with the following snippet:

#Region "PDF Converter"
        Private Function Convert_to_PDF(ByVal LicenseKey As StringByVal Source As IO.Stream) As Byte()
            Dim conv As ExpertPdf.HtmlToPdf.PdfConverter = GetPdfConverter(LicenseKey)
            Dim bytes As Byte() = conv.GetPdfBytesFromHtmlStream(Source, System.Text.Encoding.UTF8)
            conv = Nothing
            Return bytes
        End Function

        Private Function GetPdfConverter(ByVal LicenseKey As String) As ExpertPdf.HtmlToPdf.PdfConverter

            Dim pdfConverterObj As ExpertPdf.HtmlToPdf.PdfConverter = New ExpertPdf.HtmlToPdf.PdfConverter()

            If Not LicenseKey Is Nothing AndAlso LicenseKey.Length > 0 Then
pdfConverterObj.LicenseKey = LicenseKey
            End If

            pdfConverterObj.PageWidth = 0 'autodetect

            ' set if the generated PDF contains selectable text or an embedded image - default value is true
            'pdfConverterObj.PdfDocumentOptions.GenerateSelectablePdf = cbGenerateSelectablePdf.Checked

            ' set the PDF page size
            pdfConverterObj.PdfDocumentOptions.PdfPageSize = ExpertPdf.HtmlToPdf.PdfPageSize.Letter

            ' set the PDF compression level
            pdfConverterObj.PdfDocumentOptions.PdfCompressionLevel = ExpertPdf.HtmlToPdf.PdfCompressionLevel.Normal

            ' set the PDF page orientation (portrait or landscape)
            pdfConverterObj.PdfDocumentOptions.PdfPageOrientation = ExpertPdf.HtmlToPdf.PDFPageOrientation.Portrait

            ' set the PDF standard used to generate the PDF document
            pdfConverterObj.PdfStandardSubset = ExpertPdf.HtmlToPdf.PdfStandardSubset.Full

            ' show or hide header and footer
            pdfConverterObj.PdfDocumentOptions.ShowHeader = False
            pdfConverterObj.PdfDocumentOptions.ShowFooter = False

            'set the PDF document margins
            pdfConverterObj.PdfDocumentOptions.LeftMargin = 20
            pdfConverterObj.PdfDocumentOptions.RightMargin = 20
            pdfConverterObj.PdfDocumentOptions.TopMargin = 20
            pdfConverterObj.PdfDocumentOptions.BottomMargin = 20

            ' set if the HTTP links are enabled in the generated PDF
            pdfConverterObj.PdfDocumentOptions.LiveUrlsEnabled = True

            ' set if the HTML content is resized if necessary to fit the PDF page width - default is true
            pdfConverterObj.PdfDocumentOptions.FitWidth = True

            ' set if the PDF page should be automatically resized to the size of the HTML content when FitWidth is false
            pdfConverterObj.PdfDocumentOptions.AutoSizePdfPage = True

            ' embed the true type fonts in the generated PDF document
            pdfConverterObj.PdfDocumentOptions.EmbedFonts = True

            'compress the images in PDF with JPEG to reduce the PDF document size - default is true
            pdfConverterObj.PdfDocumentOptions.JpegCompressionEnabled = True

            ' set if the JavaScript is enabled during conversion 
            pdfConverterObj.ScriptsEnabled = True

            'pdfConverterObj.PdfHeaderOptions.HeaderText = ""
            'pdfConverterObj.PdfHeaderOptions.HeaderTextColor = 
            'pdfConverterObj.PdfHeaderOptions.HeaderSubtitleText = ''
            pdfConverterObj.PdfHeaderOptions.DrawHeaderLine = False

            'pdfConverterObj.PdfFooterOptions.FooterText = ""
            'pdfConverterObj.PdfFooterOptions.FooterTextColor = 
            pdfConverterObj.PdfFooterOptions.DrawFooterLine = False
            pdfConverterObj.PdfFooterOptions.PageNumberText = "Page"
            pdfConverterObj.PdfFooterOptions.ShowPageNumber = True

            'If (cbBookmarks.Checked) Then
            '    pdfConverterObj.PdfBookmarkOptions.TagNames = New String() {"h1", "h2"}
            'Else
            '    pdfConverterObj.PdfBookmarkOptions.TagNames = Nothing
            'End If
            pdfConverterObj.PdfBookmarkOptions.TagNames = Nothing

            Return pdfConverterObj
        End Function
#End Region

Average ( Ratings):
 
Want to help out?
 
 

New York, NY • Baltimore, MD • Vienna, VA • St. Louis, MO • Seattle, WA • info@openwebstudio.com

Bookmark & Share Bookmark and Share