Windows通用应用程序(UWP)中的HTML至PDF [英] HTML to PDF in a Windows universal app (UWP)

查看:110
本文介绍了Windows通用应用程序(UWP)中的HTML至PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在UWP应用中将HTML字符串转换为PDF文件? 我已经看到可以在常规.NET应用程序中完成许多不同的方法(似乎有很多第三方库),但是我还没有看到可以在Universal/UWP应用程序中完成此方法的方法.有谁知道该怎么做?

Is it possible to convert an HTML string to a PDF file in a UWP app? I've seen lots of different ways it can be done in regular .NET apps (there seem to be plenty of third party libraries), but I've yet to see a way it can be done in a Universal/UWP app. Does anyone know how it can be done?

如果没有纯代码解决方案,也许有某种方法可以插入"Microsoft Print to PDF"选项?

Perhaps there is some way to hook into the "Microsoft Print to PDF" option, if there is no pure code solution?

或者是否有一种绕行的方式,例如使用Javascript和 https://github.com/C#UWP应用程序中的MrRio/jsPDF ?我不确定,抓着稻草...

Or is there a roundabout way of doing it, maybe like somehow using Javascript and https://github.com/MrRio/jsPDF inside a C# UWP app? I'm not sure, clutching at straws...

我已将Grace Feng-MSFT提供的解决方案标记为正确,以证明可以通过使用打印对话框中的Microsoft Print to PDF选项将HTML转换为PDF.谢谢

I have marked the solution provided by Grace Feng - MSFT as correct for proving that it IS possible to convert HTML to PDF, through the use of the Microsoft Print to PDF option in the print dialog. Thank you

推荐答案

如果没有纯代码解决方案,也许有某种方法可以插入"Microsoft Print to PDF"选项?

Perhaps there is some way to hook into the "Microsoft Print to PDF" option, if there is no pure code solution?

是的,但是使用这种方式,您首先需要在RichEditBoxTextBlock之类的控件中显示HTML字符串,只有UIElement是可打印的内容.

Yes, but using this way, you will need to firstly show your HTML string in controls like RichEditBox or TextBlock, only UIElement can be printable content.

您也可以自己创建PDF文件,这是PDF中使用的基本语法:

You can also create PDF file by yourself, here is basic syntax used in PDF:

您可以使用BTET创建段落:

You can use BT and ET to create paragraph:

以下是C#中的示例:

StringBuilder sb = new StringBuilder();
sb.AppendLine("BT"); // BT = begin text object, with text-units the same as userspace-units
sb.AppendLine("/F0 40 Tf"); // Tf = start using the named font "F0" with size "40"
sb.AppendLine("40 TL"); // TL = set line height to "40"
sb.AppendLine("230.0 400.0 Td"); // Td = position text point at coordinates "230.0", "400.0"
sb.AppendLine("(Hello World)'");
sb.AppendLine("/F2 20 Tf");
sb.AppendLine("20 TL");
sb.AppendLine("0.0 0.2 1.0 rg"); // rg = set fill color to  RGB("0.0", "0.2", "1.0")
sb.AppendLine("(This is StackOverflow)'");
sb.AppendLine("ET");

然后,您可以创建PDF文件并将其保存到此文件中.但是,由于您要将HTML转换为PDF,这可能是一项艰巨的工作,我想您不想这样做.

Then you can create a PDF file and save this into this file. But since you want to convert the HTML to PDF, it could be a hard work and I think you don't want to do this.

或者是否有一种绕行的方式,也许就像使用Javascript和 https://github.com /MrRio/jsPDF 在C#UWP应用程序中?我不确定,抓着稻草...

Or is there a roundabout way of doing it, maybe like somehow using Javascript and https://github.com/MrRio/jsPDF inside a C# UWP app? I'm not sure, clutching at straws...

说实话,使用Libs或Web服务将HTML转换为PDF也是一种方法,有很多方法,我只是在搜索它们,但找不到免费的WinRT方法.因此,我认为这里最实用的方法是第一种方法,将其连接到Microsoft Print to PDF.为此,您可以检查官方的打印示例.

To be honestly, using Libs or Web service to convert HTML to PDF is also a method, there are many and I just searched for them, but I can't find any free to be used in WinRT. So I think the most practicable method here is the first one, hooking into Microsoft Print to PDF. To do this, you can check the official Printing sample.

更新: 使用@Jerry Nixon-

Update: Used @Jerry Nixon - MSFT's code in How do I print WebView content in a Windows Store App?, this is a great sample. I just added some code for add pages for printing, in the NavigationCompleted event of WebView:

private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    MyWebViewRectangle.Fill = await GetWebViewBrush(webView);
    MyPrintPages.ItemsSource = await GetWebPages(webView, new Windows.Foundation.Size(842, 595));
}

然后在printDoc.AddPages += PrintDic_AddPages;事件中(printDoc是PrintDocument的实例):

Then in the printDoc.AddPages += PrintDic_AddPages; event (printDoc is instance of PrintDocument):

private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
{
    foreach (var item in MyPrintPages.Items)
    {
        var rect = item as Rectangle;
        printDoc.AddPage(rect);
    }
    printDoc.AddPagesComplete();
}

有关其他代码,您可以参考官方印刷样本.

For other code you can refer to the official printing sample.

这篇关于Windows通用应用程序(UWP)中的HTML至PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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