This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HtmlToPdfConverter.cs
142 lines (120 loc) · 3.91 KB
/
HtmlToPdfConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
namespace WkHtmlToX
{
internal static class TupleListExtensions
{
public static void Add<T, U>(this IList<Tuple<T, U>> list, T item1, U item2)
{
list.Add(Tuple.Create(item1, item2));
}
}
public sealed class HtmlToPdfConverter : IDisposable
{
private IntPtr _globalSettings;
public HtmlToPdfConverter(PdfGlobalSettings settings = default(PdfGlobalSettings))
{
if (Native.Initialize(false))
{
_globalSettings = Native.CreateGlobalSettings();
UpdateSettings(settings);
}
}
private void UpdateSettings(PdfGlobalSettings s)
{
var update = new List<Tuple<string, string>> {
{ "size.pageSize", Enum.GetName(typeof(PageSize), s.PageSize) },
{ "orientation", Enum.GetName(typeof(Orientation), s.Orientation) },
{ "colorMode", Enum.GetName(typeof(ColorMode), s.ColorMode) },
{ "useCompression", s.UseCompression.ToString().ToLower() },
{ "margin.top", s.MarginTop },
{ "margin.right", s.MarginRight },
{ "margin.bottom", s.MarginBottom },
{ "margin.left", s.MarginLeft }
};
foreach (var setting in update)
{
Native.SetGlobalSetting(_globalSettings, setting.Item1, setting.Item2);
}
}
public byte[] ConvertToPDF(string html)
{
var converter = Native.CreateConverter(_globalSettings);
var objectSettings = Native.CreateObjectSettings();
Native.AddObject(converter, objectSettings, html);
var convertResult = Native.Convert(converter);
var data = Native.GetOutput(converter);
// destroy object settings too?
Native.DestroyConverter(converter);
return data;
}
private bool disposedValue = false; // To detect redundant calls
private void Cleanup(bool disposing)
{
if (!disposedValue)
{
// if (disposing)
// {
// // dispose managed state
// }
Native.DestroyGlobalSettings(_globalSettings);
_globalSettings = IntPtr.Zero;
Native.Destroy();
disposedValue = true;
}
}
~HtmlToPdfConverter()
{
Cleanup(false);
}
public void Dispose()
{
Cleanup(true);
GC.SuppressFinalize(this);
}
}
public class PdfGlobalSettings
{
//public Size size;
public PageSize PageSize = PageSize.Letter;
// public bool quiet;
// public bool useGraphics;
// public bool resolveRelativeLinks;
public Orientation Orientation = Orientation.Portrait;
public ColorMode ColorMode = ColorMode.Color;
// public string resolution;
// public int dpi;
// public int pageOffset;
// public int copies;
// public bool collate;
// public bool outline;
// public int outlineDepth;
// public string dumpOutline;
// public string @out;
// public string documentTitle;
public bool UseCompression = true;
//public Margin margin;
public string MarginTop = "0.5in";
public string MarginRight = "0.5in";
public string MarginBottom = "0.5in";
public string MarginLeft = "0.5in";
// public string viewportSize;
// public int imageDPI;
// public int imageQuality;
}
public enum Orientation
{
Portrait,
Landscape
}
public enum ColorMode
{
Grayscale,
Color
}
public enum PageSize
{
Letter,
Legal
}
}