UWP生成PDF-最佳方法 [英] UWP Generating a PDF - best approach

查看:289
本文介绍了UWP生成PDF-最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发UWP应用,作为一项要求,我需要动态生成PDF文件.

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:

  • 需要具有从UWP应用直接/离线生成PDF的功能
  • 可能需要在服务器上生成报告的能力-理想情况下,是使用相同的源模板
  • 报告需要具有一定的动态性(即,不能自行编译成应用程序,并且无需重新编译应用程序即可对其进行格式设置)
  • 理想情况下-不依赖于封闭源专有库/组件

关于如何处理此问题,我有一些想法:

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

  1. 拥有一个XAML页面并通过隐藏的打印对话框将该XAML视图打印为PDF来将其转换为PDF-受XAML需要编译到应用程序中的事实的限制+使用相同的方法可能不易混淆在服务器端生成报告.
  2. 使用WebView和HTML进行类似的操作-但这需要执行JavaScript逻辑-并且我不确定UWP应用中是否可能.

过去有没有人取得过类似的成就?如果是,那么您采取了什么方法?

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

推荐答案

当前,有许多第三方付费软件包可用于在UWP中生成PDF,例如 PDFSharp 在UWP中不可用.由于您不想要付费应用程序,因此您可以考虑使用免费库创建经典应用程序,然后创建一个AppService来处理它.有关更多详细信息,请参考 AppServiceBridgeSample .

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.

拥有XAML页面并通过打印将其转换为PDF

Have a XAML page and convert it PDF by printing

通过打印功能可以生成PDF做得好.但是由于您的要求通过隐藏的打印对话框"是不可能的,因此无法隐藏打印UI进行打印.

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.

使用WebView和HTML做类似的事情

Do something similar with an WebView and HTML

打开的 jsPDF 库可以帮助您通过JavaScript生成HTML到PDF.并且您可以通过

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

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);

毕竟,您可以考虑自己编写PDF文件格式.

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

这篇关于UWP生成PDF-最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆