Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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