?
Solved

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

Posted on 2011-03-15
2
Medium Priority
?
1,835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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