简体   繁体   中英

generate invoices for print from xml in c#

I have created a xml file from a json post of a rest service and want to use the information to create a pdf file for each -Node in the file/request. How do i generate such a pdf with a template and my given variables? How do i fill the variables in such a manner and how do i create seperate files? I've looked into Fo.Net but found that the project seems dead and that the templating is complex.

Then i've looked into PrinceXML and am stuck as how to use a template engine to replace a variable in my template? Could i use RazorEngine , and if so, how would the could look like?

Lastly, are there opensource alternatives that are working as good as princeXML?

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Test</title></head>
<body>
Hello World! FNR: <!-- how would one add a variable here? --><br>
<br>
</body>
</html>

XML:

<?xml version="1.0" encoding="utf-8"?>
<PdfPrinter>
  <Document>
    <FNR>FNR</FNR>
    <KNR>KNR</KNR>
    <G>G</G>
    <LN>LN</LN>
</Document>
  <Document>
    <FNR>  00</FNR>
    <KNR>555555</KNR>
    <G>1</G>
    <LN>02</LN>
</Document>
</PdfPrinter>

Using Nustache the template html should look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Test</title></head>
<body><br>
Hello World! FNR: {{fnr}}, KNR: {{knr}} Uhrzeit: {{date}} <!-- how would one add a variable here? --><br>
<br>
<br>
</body>
</html>

the {{fnr}} tags can be replaced by your own data in code via:

var data = new { knr = "123456", fnr = 10, date = DateTime.Now.ToString() };

or:

 internal class Customer
    {
        public string CustomerID {get; set;}
        public string Forename { get; set; }
        public string Surname { get; set; }
    }



Customer user = new Customer();
Render.FileToFile(templateFile, user, outputFile);

this requires the Package Nustache (so: using Nustache.Core; is necessary). It can be found here at github , or here at NuGet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM