简体   繁体   English

在C#中从xml生成打印发票

[英]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. 我已经从rest服务的json帖子中创建了一个xml文件,并希望使用该信息为文件/请求中的每个-Node创建一个pdf文件。 How do i generate such a pdf with a template and my given variables? 如何使用模板和给定的变量生成此类pdf? 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. 我研究了Fo.Net,但发现该项目似乎已经停滞 ,并且模板很复杂。

Then i've looked into PrinceXML and am stuck as how to use a template engine to replace a variable in my template? 然后,我研究了PrinceXML并被困在如何使用模板引擎替换模板中的变量上? Could i use RazorEngine , and if so, how would the could look like? 我可以使用RazorEngine ,如果可以的话,会是什么样子?

Lastly, are there opensource alternatives that are working as good as princeXML? 最后,是否有与princeXML一样出色的开源替代方案?

HTML: 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:

<?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: 使用Nustache,模板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><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: {{fnr}}标签可以通过以下方式用代码中的您自己的数据替换:

var data = new { knr = "123456", fnr = 10, date = DateTime.Now.ToString() }; 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). 这需要Package Nustache(因此:使用Nustache.Core;是必需的)。 It can be found here at github , or here at NuGet . 可以在githubNuGet处找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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