We generate PDF from an HTML template with conditions using wkhtmltopdf and RazorEngine
Despite the fact that the post offers a blank, a recipe for ASP.NET, according to a similar principle, it is possible to implement the solution for other platforms.
Task:
generate a DOC or PDF from a file template, with substitution of values
you need the ability to include minimal logic in the template to write less code, if you only need to edit external files
update . the result of the work should be saved to a file for further manipulation
Under the cut - a very simple and short solution, code examples in C #. Despite this, that the post offers a blank-recipe for ASP.NET, according to similar principles, it is possible to implement the solution for other platforms. Decision:
We generate a temporary HTML file. The file will be generated by the RazorEngine engine (can be installed via NuGet ). The code will look something like this: ')
stringtemplate = File.ReadAllText("template1.cshtml"); string result = Razor.Parse(template, new { Name = "", ShowBlock1 = false, ShowBlock2 = true }); File.WriteAllText("C:/tests/input.html", result);