b85411
b85411

Reputation: 10010

HTML to PDF in a Windows universal app (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?

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

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...

EDIT

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

Upvotes: 2

Views: 2840

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

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

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.

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

enter image description here

You can use BT and ET to create paragraph:

enter image description here

Here is sample in 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");

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.

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...

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.

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

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.

Upvotes: 2

Related Questions