-
-
Save contactsamie/61717933b30764ed153916f51f538d8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Diagnostics; | |
| using System.IO; | |
| public interface IHtmlToPdfConverter | |
| { | |
| byte[] Convert(string basePath, string htmlCode, FormatType formatType, OrientationType orientationType); | |
| } | |
| public class HtmlToPdfConverter : IHtmlToPdfConverter | |
| { | |
| public byte[] Convert(string basePath, string htmlCode, FormatType formatType = FormatType.A4, OrientationType orientationType = OrientationType.Portrait) | |
| { | |
| var inputFileName = $"input_{Guid.NewGuid()}.html"; | |
| var outputFileName = $"output_{Guid.NewGuid()}.pdf"; | |
| File.WriteAllText($"{basePath}/{inputFileName}", htmlCode); | |
| var startInfo = new ProcessStartInfo("phantomjs.exe") | |
| { | |
| WorkingDirectory = basePath, | |
| Arguments = $"rasterize.js \"{inputFileName}\" \"{outputFileName}\" \"{formatType}\" \"{orientationType.ToString().ToLower()}\"", | |
| UseShellExecute = true, | |
| }; | |
| var process = new Process { StartInfo = startInfo }; | |
| process.Start(); | |
| process.WaitForExit(); | |
| var bytes = File.ReadAllBytes($"{basePath}/{outputFileName}"); | |
| File.Delete($"{basePath}/{inputFileName}"); | |
| File.Delete($"{basePath}/{outputFileName}"); | |
| return bytes; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment