|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using DinkToPdf.Contracts; |
| 7 | +using DinkToPdf.EventDefinitions; |
| 8 | +using System.Globalization; |
| 9 | + |
| 10 | +namespace DinkToPdf |
| 11 | +{ |
| 12 | + public class BasicConverter : IConverter |
| 13 | + { |
| 14 | + public readonly ITools Tools; |
| 15 | + |
| 16 | + public IDocument ProcessingDocument { get; private set; } |
| 17 | + |
| 18 | + public event EventHandler<PhaseChangedArgs> PhaseChanged; |
| 19 | + |
| 20 | + public event EventHandler<ProgressChangedArgs> ProgressChanged; |
| 21 | + |
| 22 | + public event EventHandler<FinishedArgs> Finished; |
| 23 | + |
| 24 | + public event EventHandler<ErrorArgs> Error; |
| 25 | + |
| 26 | + public event EventHandler<WarningArgs> Warning; |
| 27 | + |
| 28 | + public BasicConverter(ITools tools) { |
| 29 | + Tools = tools; |
| 30 | + } |
| 31 | + |
| 32 | + public virtual byte[] Convert(IDocument document) |
| 33 | + { |
| 34 | + if (document.GetObjects().Count() == 0) |
| 35 | + { |
| 36 | + throw new ArgumentException("No objects is defined in document that was passed. At least one object must be defined."); |
| 37 | + } |
| 38 | + |
| 39 | + ProcessingDocument = document; |
| 40 | + |
| 41 | + byte[] result = new byte[0]; |
| 42 | + Tools.Load(); |
| 43 | + |
| 44 | + IntPtr converter = CreateConverter(document); |
| 45 | + |
| 46 | + //register events |
| 47 | + Tools.SetPhaseChangedCallback(converter, OnPhaseChanged); |
| 48 | + Tools.SetProgressChangedCallback(converter, OnProgressChanged); |
| 49 | + Tools.SetFinishedCallback(converter, OnFinished); |
| 50 | + Tools.SetWarningCallback(converter, OnWarning); |
| 51 | + Tools.SetErrorCallback(converter, OnError); |
| 52 | + |
| 53 | + bool converted = Tools.DoConversion(converter); |
| 54 | + |
| 55 | + if (converted) |
| 56 | + { |
| 57 | + result = Tools.GetConversionResult(converter); |
| 58 | + } |
| 59 | + |
| 60 | + Tools.DestroyConverter(converter); |
| 61 | + |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + private void OnPhaseChanged(IntPtr converter) |
| 66 | + { |
| 67 | + int currentPhase = Tools.GetCurrentPhase(converter); |
| 68 | + var eventArgs = new PhaseChangedArgs() |
| 69 | + { |
| 70 | + Document = ProcessingDocument, |
| 71 | + PhaseCount = Tools.GetPhaseCount(converter), |
| 72 | + CurrentPhase = currentPhase, |
| 73 | + Description = Tools.GetPhaseDescription(converter, currentPhase) |
| 74 | + }; |
| 75 | + |
| 76 | + PhaseChanged?.Invoke(this, eventArgs); |
| 77 | + } |
| 78 | + |
| 79 | + private void OnProgressChanged(IntPtr converter) |
| 80 | + { |
| 81 | + var eventArgs = new ProgressChangedArgs() |
| 82 | + { |
| 83 | + Document = ProcessingDocument, |
| 84 | + Description = Tools.GetProgressString(converter) |
| 85 | + }; |
| 86 | + |
| 87 | + ProgressChanged?.Invoke(this, eventArgs); |
| 88 | + } |
| 89 | + |
| 90 | + private void OnFinished(IntPtr converter, int success) |
| 91 | + { |
| 92 | + var eventArgs = new FinishedArgs() |
| 93 | + { |
| 94 | + Document = ProcessingDocument, |
| 95 | + Success = success == 1 ? true : false |
| 96 | + }; |
| 97 | + |
| 98 | + Finished?.Invoke(this, eventArgs); |
| 99 | + } |
| 100 | + |
| 101 | + private void OnError(IntPtr converter, string message) |
| 102 | + { |
| 103 | + var eventArgs = new ErrorArgs() |
| 104 | + { |
| 105 | + Document = ProcessingDocument, |
| 106 | + Message = message |
| 107 | + }; |
| 108 | + |
| 109 | + Error?.Invoke(this, eventArgs); |
| 110 | + } |
| 111 | + |
| 112 | + private void OnWarning(IntPtr converter, string message) |
| 113 | + { |
| 114 | + var eventArgs = new WarningArgs() |
| 115 | + { |
| 116 | + Document = ProcessingDocument, |
| 117 | + Message = message |
| 118 | + }; |
| 119 | + |
| 120 | + Warning?.Invoke(this, eventArgs); |
| 121 | + } |
| 122 | + |
| 123 | + private IntPtr CreateConverter(IDocument document) { |
| 124 | + |
| 125 | + IntPtr converter = IntPtr.Zero; |
| 126 | + |
| 127 | + { |
| 128 | + IntPtr settings = Tools.CreateGlobalSettings(); |
| 129 | + |
| 130 | + ApplyConfig(settings, document, true); |
| 131 | + |
| 132 | + converter = Tools.CreateConverter(settings); |
| 133 | + } |
| 134 | + |
| 135 | + foreach (var obj in document.GetObjects()) |
| 136 | + { |
| 137 | + if (obj != null) |
| 138 | + { |
| 139 | + IntPtr settings = Tools.CreateObjectSettings(); |
| 140 | + |
| 141 | + ApplyConfig(settings, obj, false); |
| 142 | + |
| 143 | + Tools.AddObject(converter, settings, obj.GetContent()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return converter; |
| 148 | + } |
| 149 | + |
| 150 | + private void ApplyConfig(IntPtr config, ISettings settings, bool isGlobal) { |
| 151 | + |
| 152 | + if (settings == null) |
| 153 | + { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; |
| 158 | + |
| 159 | + var props = settings.GetType().GetProperties(bindingFlags); |
| 160 | + |
| 161 | + foreach (var prop in props) |
| 162 | + { |
| 163 | + Attribute[] attrs = (Attribute[])prop.GetCustomAttributes(); |
| 164 | + object propValue = prop.GetValue(settings); |
| 165 | + |
| 166 | + if (propValue == null) |
| 167 | + { |
| 168 | + continue; |
| 169 | + } |
| 170 | + else if (attrs.Length > 0 && attrs[0] is WkHtmlAttribute) |
| 171 | + { |
| 172 | + var attr = attrs[0] as WkHtmlAttribute; |
| 173 | + |
| 174 | + Apply(config, attr.Name, propValue, isGlobal); |
| 175 | + } |
| 176 | + else if (propValue is ISettings) |
| 177 | + { |
| 178 | + ApplyConfig(config, propValue as ISettings, isGlobal); |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private void Apply(IntPtr config, string name, object value, bool isGlobal) |
| 185 | + { |
| 186 | + var type = value.GetType(); |
| 187 | + |
| 188 | + Func<IntPtr, string, string, int> applySetting; |
| 189 | + if (isGlobal) |
| 190 | + { |
| 191 | + applySetting = Tools.SetGlobalSetting; |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + applySetting = Tools.SetObjectSetting; |
| 196 | + } |
| 197 | + |
| 198 | + if (typeof(bool) == type) |
| 199 | + { |
| 200 | + applySetting(config, name, ((bool)value == true ? "true" : "false")); |
| 201 | + } |
| 202 | + else if (typeof(double) == type) |
| 203 | + { |
| 204 | + applySetting(config, name, ((double)value).ToString("0.##", CultureInfo.InvariantCulture)); |
| 205 | + } |
| 206 | + else if (typeof(Dictionary<string, string>).IsAssignableFrom(type)) |
| 207 | + { |
| 208 | + var dictionary = (Dictionary<string, string>)value; |
| 209 | + int index = 0; |
| 210 | + |
| 211 | + foreach (var pair in dictionary) |
| 212 | + { |
| 213 | + if (pair.Key == null || pair.Value == null) |
| 214 | + { |
| 215 | + continue; |
| 216 | + } |
| 217 | + |
| 218 | + //https://github.com/wkhtmltopdf/wkhtmltopdf/blob/c754e38b074a75a51327df36c4a53f8962020510/src/lib/reflect.hh#L192 |
| 219 | + applySetting(config, name + ".append", null); |
| 220 | + applySetting(config, string.Format("{0}[{1}]", name, index), pair.Key + "\n" + pair.Value); |
| 221 | + |
| 222 | + index++; |
| 223 | + } |
| 224 | + } |
| 225 | + else |
| 226 | + { |
| 227 | + applySetting(config, name, value.ToString()); |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments