Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using CK editor in iframes 5 28
VB.NET Inline If statement 4 41
Query in AngularJS 8 52
Vb.net progressbar with timer1 in visual studio win app 11 38
Power PDF (http://www.nuance.com/for-business/document-imaging-and-scanning/power-pdf-converter/index.htm) is the newest product from the Document Imaging division of Nuance Communications (http://www.nuance.com/). It is available in two editions — …
*Adobe Acrobat 9 was used for this article. Particular steps may vary depending on software versions. 1. Create a framework of your form in Word, leaving space where you’d ultimately like the Adobe fields to appear.  (Note: I use the blank lines …
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…
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…

733 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