Solved

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

Posted on 2011-03-15
2
1,751 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
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.
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…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

943 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

4 Experts available now in Live!

Get 1:1 Help Now