PDF creator COM-object based on TheArtOfDev.HtmlRenderer.PdfSharp
As base of code I take https://github.com/ArthurHub/HTML-Renderer, this is awesome project and I use it as base in number of my projects:
- Multi Languages Spell Checker for webmaster. Part 4. TheArtOfDev.HtmlRenderer.
- TreeView FileSelector by ReactiveNET and TheArtOfDev.HtmlRenderer (SpellChecker project).
Full source code of this COM-object I have uploaded this project with test to Github https://github.com/Alex-1557/Html2PdfComObject.
This COM-object, of course, can working in any desktop application as ordinary assembly, not only on ancient ASP web-page.
This is a simplest way to perform my COM-object on ASP page.
1: <!DOCTYPE html>
2: <html>
3: <body>
4: <form name='TDS' method='post' ID="test1" action="/TSTPDF2.asp">
5: <a href="#">Test PDF component.</a> <br><br>
6: Html file<br>
7: <input type="text" name="html" style="width: 500px;" value="H:\TMP2\tst2.htm">
8:
9: <br>
10: <br>
11: <br>
12: <input type="submit" value="Create PDF">
13: </form>
14: <%
15: Const ForReading = 1
16: Const ForWriting = 2
17: Const ForAppending = 8
18:
19: If IsPostBack() then
20: Dim PdfCretor, FS, File 'as object
21: Dim TmpDir, FileContent, GUID, TmpFileName 'as string
22:
23: Set FS = Server.CreateObject("Scripting.FileSystemObject")
24: Set File = FS.OpenTextFile(Request.Form("html"), ForReading)
25: FileContent = File.ReadAll
26: File.Close
27:
28: TmpDir = Server.MapPath("temp")
29: GUID = CreateGUID()
30: TmpFileName = TmpDir & "\" & GUID & ".pdf"
31:
32: Set PdfCretor = Server.CreateObject("TDS.HTML2PDF")
33: PdfCretor.CreatePdfFile2 TmpFileName, FileContent, 4,10
34:
35: Response.Redirect("/temp/" & GUID & ".pdf")
36:
37:
38: Set PdfCretor = nothing
39: Set FS = nothing
40: Set File = nothing
41: end if
42:
43: Function CreateGUID
44: Dim TypeLib
45: Set TypeLib = CreateObject("Scriptlet.TypeLib")
46: CreateGUID = Mid(TypeLib.Guid, 2, 36)
47: End Function
48:
49: Function IsPostBack()
50: If Request.ServerVariables("Request_Method") = "POST" Then
51: IsPostBack="True"
52: Else
53: IsPostBack="False"
54: End If
55: End Function
56:
57:
58: %>
59: </body>
60: </html>
In Github you can see TestProject how to use my COM-object as Type of Assembly and as COM-object.
1: Imports System.Net
2: Imports System.Security.Policy
3: Imports System.Text
4: Imports Microsoft.VisualStudio.TestTools.UnitTesting
5: Imports Microsoft.VisualStudio.TestTools.UnitTesting.Logging
6: Imports NLog
7: Imports PdfSharp
8: Imports PdfSharp.Drawing
9: Imports TDSprint2
10: Imports TheArtOfDev.HtmlRenderer.Adapters
11: Imports TheArtOfDev.HtmlRenderer.Core
12: Imports TheArtOfDev.HtmlRenderer.PdfSharp
13: 'Imports PageSize = PdfSharp.PageSize
14: Imports PageSize = TDSprint2.PageSize
15:
16: <TestClass()> Public Class UnitTest1
17: Friend Request As MyWebClient
18: Friend Shared Log As NLog.Logger = LogManager.GetCurrentClassLogger()
19:
20: <TestInitialize>
21: Public Sub Start()
22: Request = New MyWebClient
23: End Sub
24:
25:
26: <TestMethod()>
27: <DataRow("H:\TMP2\tst2.htm")>
28: Public Sub TestClass(FileName As String)
29: Try
30: Dim Response As String = IO.File.ReadAllText(FileName)
31: Dim TmpDir As String = System.IO.Path.GetTempPath
32: Dim TmpFile As String = IO.Path.Combine(TmpDir, Replace(FileName, ".htm", ".pdf"))
33: Dim X As New Html2Pdf
34: Dim Config As New TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerateConfig With {
35: .MarginBottom = 10,
36: .MarginLeft = 10,
37: .MarginRight = 10,
38: .MarginTop = 10,
39: .PageOrientation = 0,
40: .PageSize = 4
41: }
42: Dim Css As CssData = X.ParseStyleSheet("", True)
43: X.CreatePdfFile3(TmpFile, Response, 4, 10, Css)
44: Dim Ret2 = Process.Start("C:\Program Files\Mozilla Firefox\firefox.exe", TmpFile)
45: Catch ex As WebException
46: Dim Resp As String = ""
47: Dim Stream = ex.Response?.GetResponseStream()
48: If Stream IsNot Nothing Then
49: Dim Sr = New IO.StreamReader(Stream)
50: Resp = Sr.ReadToEnd
51: End If
52: Log.Info(Resp & vbCrLf & ex.Message)
53: End Try
54: End Sub
55:
56: <DataRow("H:\TMP2\tst2.htm")>
57: <TestMethod()>
58: Public Sub TestCOM(FileName As String)
59: Try
60: Dim Response As String = IO.File.ReadAllText(FileName)
61: Dim TmpDir As String = System.IO.Path.GetTempPath
62: Dim TmpFile As String = IO.Path.Combine(TmpDir, Replace(FileName, ".htm", ".pdf"))
63: '
64: Dim Config As New TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerateConfig With {
65: .MarginBottom = 10,
66: .MarginLeft = 10,
67: .MarginRight = 10,
68: .MarginTop = 10,
69: .PageOrientation = 0,
70: .PageSize = 4
71: }
72: Dim ComObjType = Type.GetTypeFromProgID("TDS.Html2Pdf")
73: Dim X As IHtml2Pdf = Activator.CreateInstance(ComObjType)
74: X.CreatePdfFile2(TmpFile, Response, 4, 10)
75: Dim Ret2 = Process.Start("C:\Program Files\Mozilla Firefox\firefox.exe", TmpFile)
76: Catch ex As WebException
77: Dim Resp As String = ""
78: Dim Stream = ex.Response?.GetResponseStream()
79: If Stream IsNot Nothing Then
80: Dim Sr = New IO.StreamReader(Stream)
81: Resp = Sr.ReadToEnd
82: End If
83: Log.Info(Resp & vbCrLf & ex.Message)
84: End Try
85: End Sub
86:
87: End Class
About deploying this COM-object to server you can read in page How to create and testing COM-object of ancient ASP web-sites - list of tools and description of all workflow..
Comments (
)
Link to this page:
http://www.vb-net.com/Html2PdfComObject/Index.htm
|