Solved

Ghostscript Wrapper for VB.NET to use for printing HTML to PDF from web server

Posted on 2011-03-15
2
1,737 Views
Last Modified: 2012-06-21
Noticed that most PDF printers use Ghostscript to convert printer output to PDF. Was wondering how difficult it would be to use the Ghostcript.dll to create a control that inputs HTML and outputs PDF to a browser or hard drive, which can then be pushed out to a browser
0
Comment
Question by:shelbyinfotech
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35148508
ITextSharp is an open source library for working with PDF.  I believe that it has what you need, as shown in the example:

http://www.mindfiresolutions.com/HTML-to-PDF-conversion-using-iTextsharp-1088.php

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf

Public Class PdfService

    ''' <summary>
    ''' Read an HTML file, and output a matching PDF file.
    ''' </summary>
    ''' <param name="htmlFile">The HTML file name.</param>
    ''' <param name="pdfFile">The PDF file name.</param>
    ''' <param name="overwriteExisting">if set to <c>true</c> overwrite existing file.</param>
    Public Shared Sub HtmlToPdf(htmlFile As String, pdfFile As String, overwriteExisting As Boolean, settings As DocumentSettings)

        If Not File.Exists(htmlFile) Then
            Throw New FileNotFoundException("Input HTML file missing", htmlFile)
        End If

        If File.Exists(pdfFile) AndAlso Not overwriteExisting AndAlso
            MessageBox.Show("Overwrite existing PDF file?", "Create PDF", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then

            Dim htmlText As String = File.ReadAllText(htmlFile)

            Dim document As New Document(settings.PageSize,
                                         settings.LeftMargin, settings.RightMargin,
                                         settings.TopMargin, settings.BottomMargin)

            Using outputStream As New FileStream(pdfFile, FileMode.Create)

                PdfWriter.GetInstance(document, outputStream)

                Using stringReader As New StringReader(htmlText)
                    Dim parsedList As List(Of IElement) = HTMLWorker.ParseToList(stringReader, Nothing)
                    document.Open()

                    ' Parse each HTML element, and add it to the PDF document
                    For Each item As IElement In parsedList
                        document.Add(item)
                    Next

                    document.Close()

                End Using

            End Using
        End If

    End Sub

    Public Class DocumentSettings
        Public Property PageSize As Rectangle
        Public Property LeftMargin As Integer
        Public Property TopMargin As Integer
        Public Property RightMargin As Integer
        Public Property BottomMargin As Integer
    End Class

End Class

Open in new window

0
 

Author Closing Comment

by:shelbyinfotech
ID: 35148936
WOW !

I was just looking to get started here and you gave me a complete solution

Wish I could send more than 500 points
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now