0

I have a webview correctly displaying the HTML content. When loading a lot of information the webview still displays correctly, but the generated PDF stops adding the content and only adds white space.

If i scroll through the webview before generating the PDF, the second half of the PDF will be included in the document while the first part is ignored and resulting in white space.

view.Measure(
View.MeasureSpec.MakeMeasureSpec( ViewGroup.LayoutParams.WrapContent,MeasureSpecMode.Unspecified), 
 View.MeasureSpec.MakeMeasureSpec(ViewGroup.LayoutParams.WrapContent, MeasureSpecMode.Unspecified));
            view.Layout(0, 0, view.MeasuredWidth, view.MeasuredHeight);


            var printAttributesBuilder = new PrintAttributes.Builder();
            printAttributesBuilder.SetColorMode(PrintColorMode.Color);
            printAttributesBuilder.SetMediaSize(PrintAttributes.MediaSize.IsoA4);
            printAttributesBuilder.SetDuplexMode(DuplexMode.None);
            printAttributesBuilder.SetMinMargins(PrintAttributes.Margins.NoMargins);


            PdfDocument document = new PrintedPdfDocument(view.Context, printAttributesBuilder.Build());


            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(view.MeasuredWidth, view.MeasuredHeight, 1).Create();


            PdfDocument.Page page = document.StartPage(pageInfo);


            view.Draw(page.Canvas);


            document.FinishPage(page);


            byte[] docBytes = null;


            using (MemoryStream memoryStream = new MemoryStream())
            {
                try
                {
                    document.WriteTo(memoryStream);


                    docBytes = memoryStream.ToArray();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }


            return docBytes;

and also

WebView.EnableSlowWholeDocumentDraw();

I believe that the size of the PDF is correct, somehow only the content stops from adding. The remaining white space takes aproximately the same space like the missing content would. I tried using the PrintManager and that generates a 30 page PDF based on the same HTML. But I do not want to use PrintManager or any other libraries, i have to make the posted solution work.

Any input is highly appreciated!

  • The code posted looks correct. The code is creating one page so it looks like the issue is with the code that combines the pages. – jdweng Mar 25 '19 at 11:29
  • I can not split the content of the webview. Can you provide an example with a solution on how to create multiple pages instead of one? – Alex Constantin Mar 25 '19 at 12:25
  • Try following : https://stackoverflow.com/questions/34216833/open-several-pages-in-webview-and-create-pdf – jdweng Mar 25 '19 at 12:30
  • I will check. Thank you! – Alex Constantin Mar 25 '19 at 12:37
  • I looked at the solution you pointed to, but the problem is i'm only loading the content once and i have no base for splitting it. I am using the same webview that has all the information. It will be ideal to split it in pages. – Alex Constantin Mar 26 '19 at 08:13
  • Are you getting a status in the response of 200? – jdweng Mar 26 '19 at 08:54
  • I do not make any request. I load a local HTML file. – Alex Constantin Mar 26 '19 at 11:44
  • See examples at URL shown. I suspect you have one pdf page and your code is designed for multiple pages and you have StartPage and FinishPage. The example use one page : https://csharp.hotexamples.com/examples/-/PdfDocument/-/php-pdfdocument-class-examples.html – jdweng Mar 26 '19 at 12:08
  • The majority of them are with 3rd party packages. They are not helpful. And the problem still persist. – Alex Constantin Mar 27 '19 at 13:47
  • What are you using? – jdweng Mar 27 '19 at 14:14
  • i'm not using any packages besides native code. Do you know anything about a maximum size or allowed content for a webview or a stream? I tried a similar approach to the one posted using bitmap and the content that is captured from the webview stops at the very same line of text. Could the webview itself be the cause of this problem? Why is it not scrolling until the bottom? – Alex Constantin Mar 27 '19 at 14:40
  • See section 2.17 on following for creating the memory steam : https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version-1-22#DocumentDestination – jdweng Mar 27 '19 at 14:53
  • Thanks for the latest suggestion, but that solution translates to exactly my code but also writing to a file stream which returns the exact same result. I still believe that the webview is not able of rendering past a specific point in its content. I also tried creating bitmaps from the webview and again... Stops at the same point in the content. Do you know another way of generating the pdf and having access to the file without any libraries? Many thanks – Alex Constantin Mar 28 '19 at 08:04
  • Even though you are opening an html file there is still a connection to run the html. You are calling a create method to open the html. But that is only reading the html into application. The last link I provided in 2.7 says "PDF reader will activate the default web browser navigating to the desired web page." So navigating is the request to the html page. So there should be a response with a status. If you open a sniffer like wireshark or fiddler you should be able to see the status. If is stopping the pdf may have issues or not be compatible version with your pdfDocument. – jdweng Mar 28 '19 at 09:10
  • Is it possible the bitmap graphic resolution that the pdf document isn't supported by the graphics card on the PC? See following article : https://windowsreport.com/pdf-files-not-printing-properly-windows-10/ – jdweng Mar 28 '19 at 09:19

0 Answers0