This repository was archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathConvertOptions.cs
More file actions
179 lines (151 loc) · 5.58 KB
/
ConvertOptions.cs
File metadata and controls
179 lines (151 loc) · 5.58 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Wkhtmltopdf.NetCore.Options;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Wkhtmltopdf.NetCore.Interfaces;
namespace Wkhtmltopdf.NetCore
{
public class ConvertOptions : IConvertOptions
{
public ConvertOptions()
{
this.PageMargins = new Margins();
this.EnableJavaScript = true;
}
/// <summary>
/// Sets the page size.
/// </summary>
[OptionFlag("-s")]
public Size? PageSize { get; set; }
/// <summary>
/// Sets the page width in mm.
/// </summary>
/// <remarks>Has priority over <see cref="PageSize"/> but <see cref="PageHeight"/> has to be also specified.</remarks>
[OptionFlag("--page-width")]
public double? PageWidth { get; set; }
/// <summary>
/// Sets the page height in mm.
/// </summary>
/// <remarks>Has priority over <see cref="PageSize"/> but <see cref="PageWidth"/> has to be also specified.</remarks>
[OptionFlag("--page-height")]
public double? PageHeight { get; set; }
/// <summary>
/// Sets the page orientation.
/// </summary>
[OptionFlag("-O")]
public Orientation? PageOrientation { get; set; }
/// <summary>
/// Sets the page margins.
/// </summary>
public Margins PageMargins { get; set; }
/// <summary>
/// Indicates whether the PDF should be generated with forms.
/// </summary>
[OptionFlag("--enable-forms")]
public bool EnableForms { get; set; }
protected string GetContentType()
{
return "application/pdf";
}
/// <summary>
/// Indicates whether the PDF should be generated in lower quality.
/// </summary>
[OptionFlag("-l")]
public bool IsLowQuality { get; set; }
/// <summary>
/// Number of copies to print into the PDF file.
/// </summary>
[OptionFlag("--copies")]
public int? Copies { get; set; }
/// <summary>
/// Indicates whether the PDF should be generated in grayscale.
/// </summary>
[OptionFlag("-g")]
public bool IsGrayScale { get; set; }
/// <summary>
/// Path to header HTML file.
/// </summary>
[OptionFlag("--header-html")]
public string HeaderHtml { get; set; }
/// <summary>
/// Sets the header spacing.
/// </summary>
[OptionFlag("--header-spacing")]
public int? HeaderSpacing { get; set; }
/// <summary>
/// Path to footer HTML file.
/// </summary>
[OptionFlag("--footer-html")]
public string FooterHtml { get; set; }
/// <summary>
/// Sets the footer spacing.
/// </summary>
[OptionFlag("--footer-spacing")]
public int? FooterSpacing { get; set; }
/// <summary>
/// Do not allow web pages to run JavaScript
/// </summary>
/// <remarks>Disabled by default</remarks>
[OptionFlag("--disable-javascript")]
public bool DisableJavaScript { get; set; }
/// <summary>
/// Allow web pages to run JavaScript
/// </summary>
[OptionFlag("--enable-javascript")]
public bool EnableJavaScript { get; set; }
/// <summary>
/// Waits this number of milliseconds for JavaScript to finish executing
/// </summary>
/// <remarks>Default is 200ms</remarks>
[OptionFlag("--javascript-delay")]
public int? JavaScriptDelay { get; set; }
/// <summary>
/// Sets the variables to replace in the header and footer html
/// </summary>
/// <remarks>Replaces [name] with value in header and footer (repeatable).</remarks>
[OptionFlag("--replace")]
public Dictionary<string, string> Replacements { get; set; }
public string GetConvertOptions()
{
var result = new StringBuilder();
if (this.PageMargins != null)
result.Append(this.PageMargins.ToString());
result.Append(" ");
result.Append(GetConvertBaseOptions());
return result.ToString().Trim();
}
protected string GetConvertBaseOptions()
{
var result = new StringBuilder();
var fields = this.GetType().GetProperties();
foreach (var fi in fields)
{
var of = fi.GetCustomAttributes(typeof(OptionFlag), true).FirstOrDefault() as OptionFlag;
if (of == null)
continue;
object value = fi.GetValue(this, null);
if (value == null)
continue;
if (fi.PropertyType == typeof(Dictionary<string, string>))
{
var dictionary = (Dictionary<string, string>)value;
foreach (var d in dictionary)
{
result.AppendFormat(" {0} \"{1}\" \"{2}\"", of.Name, d.Key, d.Value);
}
}
else if (fi.PropertyType == typeof(bool))
{
if ((bool)value)
result.AppendFormat(CultureInfo.InvariantCulture, " {0}", of.Name);
}
else
{
result.AppendFormat(CultureInfo.InvariantCulture, " {0} {1}", of.Name, value);
}
}
return result.ToString().Trim();
}
}
}