2

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

b85411
  • 9,420
  • 15
  • 65
  • 119

1 Answers1

2

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.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Hi @Grace Feng - MSFT - thank you for your reply. Thanks for the PDF syntax. If I can't get the Microsoft Print to PDF option working I'll probably have to do it that way. – b85411 Aug 23 '16 at 04:35
  • @b85411, please check the official sample I provided in the end of my answer, you can get the HTML string and write the string into a `TextBlock` or some other controls and print this control. If you meet some problem when developing PDF printer, you can post your code. – Grace Feng Aug 23 '16 at 04:43
  • Is it essential to use something like a TextBlock? What I have done so far is to convert a webview into a series of webviewbrushes (`FrameworkElement`s or more specifically they are `Rectangles`). Now that I have basically drawn the parsed HTML onto a Rectangle, how difficult is it to then print those Rectangles? I found the Github examples you linked to last week and have been trying to adapt it... it's all so reliant on things like RichTextBlockOverflow and that's where I am running into problems. Can't I just print one `Rectangle` per page without worrying about overflow? Thank you – b85411 Aug 23 '16 at 04:51
  • No, it is not essential to use something like `TextBlock`, and if I correctly understood your method, you're convert webview into brushes and fill `Rectangle` with these brushes? I think it is also doable. And using this method should have no overflow problem? I'm sorry that I'm actually not very sure about this, cause I haven't try this method, but I think it worth giving a try. – Grace Feng Aug 23 '16 at 05:24
  • Yes that is correct. Where I am falling down though is that the samples seem to be so heavily reliant on RichTextBoxOverflow, it's not really clear how it can be done with Rectangles. For example - https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/Printing/cs/PrintHelper.cs#L183 . Thank you – b85411 Aug 23 '16 at 05:42
  • @b85411, I used Jerry Nixon - MSFT's code and print webview for test, it works, I've updated my answer, you can have a check. – Grace Feng Aug 23 '16 at 12:02
  • thanks very much for your updated answer. I am now able to get it working somewhat. When you tested it, did you find it was splitting pages up incorrectly? For example, when I try to print (to pdf) a large web page that would have a height of about 10 pages it seems to be doubling up. Like most of page 1 appears on page 2, most of page 2 appears on page 3 and so on - it only seems to be adding a little bit more new content on each page rather than a clean page break. I hope that makes sense. – b85411 Aug 24 '16 at 06:49
  • @b85411, no, I didn't find this problem, is that related to your rectangle's size? I don't know, is the items correctly be shown? If the items have no such problem, I think it is possible controlled by system and we can't modify it. Maybe you can post a web link so I can test it. And it seems we've discussed too much in the comments, it is suggested to move discussion to chat... – Grace Feng Aug 24 '16 at 07:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121692/discussion-between-b85411-and-grace-feng-msft). – b85411 Aug 24 '16 at 07:18