How do I use it?
- Download
wkhtmltopdf.exe
- Create
your HTML document that you want to convert into a PDF
- Run your HTML document through the tool.
Create Class WKHtmlToPdfClass.vb
Download wkhtmltopdf.exe and save in Folder
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Security
Imports System.Web
Imports System.Web.Hosting
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Public Class WKHtmlToPdfClass
Public Shared Function WKHtmlToPdf(ByVal url As String) As Byte()
Dim fileName = " - "
Dim wkhtmlDir = "C:\"
Dim wkhtml = HttpContext.Current.Server.MapPath("~/FolderName/") & "wkhtmltopdf.exe"
Dim p = New Process
p.StartInfo.CreateNoWindow
= True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardInput
= True
p.StartInfo.UseShellExecute = False
p.StartInfo.FileName =
wkhtml
p.StartInfo.WorkingDirectory = wkhtmlDir
Dim switches As String = ""
switches = (switches + "--print-media-type ")
switches = (switches + "--margin-top 10mm --margin-bottom 10mm
--margin-right 10mm --margin-left 10mm ")
switches = (switches + "--page-size Letter ")
p.StartInfo.Arguments =
(switches + (" " _
+ (url + (" " +
fileName))))
p.Start()
'read output
Dim buffer() As Byte = New Byte((32768) - 1) {}
Dim file() As Byte
Dim ms = New MemoryStream
While True
Dim read As Integer =
p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)
If (read <= 0) Then
Exit While
End If
ms.Write(buffer, 0, read)
End While
file = ms.ToArray
' wait or exit
p.WaitForExit(60000)
' read the exit code, close process
Dim returnCode As Integer = p.ExitCode
p.Close()
'Return (returnCode = 0)
Return If(returnCode = 0, file, Nothing)
'TODO: Warning!!!, inline IF is not supported ?
'TODO: Warning!!!! NULL EXPRESSION DETECTED...
End Function
End Class
Create default.aspx.vb
Public Sub HTMLtoPDF()
Dim url = "http://yoursite/FolderName/test.html"
Dim file = WKHtmlToPdfClass.WKHtmlToPdf(url, TemplateName)
If (Not (file) Is Nothing) Then
Response.ContentType =
"Application/pdf"
Response.BinaryWrite(file)
Response.End()
End If
End Sub
No comments:
Post a Comment