UWP Generating a PDF - best approach

百般思念 提交于 2020-01-03 05:50:26

问题


I'm working on a UWP app and as one of the requirements I need to generate a PDF file dynamically.

There are a few contraints / requirements that I need to concider:

  • Need an ability to generate PDF from the UWP app directly / offline scenario
  • Potentially need an ability to generate the report on the server - ideally from the same source template
  • Report needs to be somewhat dynamic (i.e. not compiled into the app it self and format editable without recompilation of the app)
  • Ideally - not depend on a closed source propetiary library / component

I have had couple of ideas on how to approach this:

  1. Have a XAML page and convert it PDF by printing that XAML view as a PDF through a hidden print dialog - limited by the fact the XAML will need to be compiled into the app + would probably be imposisble to use the same approach to generate reports on the server side.
  2. Do something similar with an WebView and HTML - but that will require JavaScript logic to be executed - adn I'm not sure if it is possible in UWP app.

Has anyone achieved something similar in the past and if so - what approach have you taken?


回答1:


Currently, there are many third party paid packages for generating PDF in UWP, such as Syncfusion and XFinium. For free libraries like PDFSharp that are not available in UWP. Since you don't want a paid one, you may consider to create classical application with free libraries and then create an AppService to handle it. For more details please reference AppServiceBridgeSample.

Have a XAML page and convert it PDF by printing

Generate the PDF by printing feature can work well. But for your requirements "through a hidden print dialog" is not possible, print UI cannot be hidden to print.

Do something similar with an WebView and HTML

The open jsPDF library can help you generate HTML to PDF by JavaScript. And you can get the PDF result return by InvokeScriptAsync from WebView. For example:

JavaScript

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script> 
    <script type='text/javascript'> 
        function print() {
            var doc = new jsPDF()
            doc.text('Hello world!', 10, 10);
            doc.circle(50, 50, 50);
            var pdfresult = doc.output(); 
            document.getElementById('myDiv').innerText = pdfresult; 
            //doc.save('a4.pdf');
            return pdfresult;
        }
    </script>
</head>
<body>
    <div id='myDiv'>Hello</div> 
</body>
</html>

WebView

Webview.Navigate(new Uri("ms-appx-web:///Assets/test.html"));
string pdfresult = await Webview.InvokeScriptAsync("eval", new string[] { "print()" });
StorageFile pdf = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.pdf");
await FileIO.WriteTextAsync(pdf, pdfresult);

After all, you can consider Write the PDF file format yourself. Here is a document for windows phone but you may still reference.



来源:https://stackoverflow.com/questions/45496324/uwp-generating-a-pdf-best-approach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!