There is an HTML page consisting of 3 tables (empty), which are filled in with JavaScript with a certain periodicity (the data of the tables are updated). The page is forgiven using the WebBrowser in a WPF application. It is necessary to generate a PDF document in which the page content will be at a certain point in time (filled tables) What libraries can do this?

I tried to do the first two ways through ExecWB

 IOleServiceProvider sp = Browser.Document as IOleServiceProvider; if (sp != null) { Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046"); Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E"); const int OLECMDID_PRINT = 6; const int OLECMDEXECOPT_DONTPROMPTUSER = 2; const short PRINT_WAITFORCOMPLETION = 2; dynamic wb; sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, out wb); if (wb != null) { wb.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION,null,null); } } 

Instead of nulls, you can substitute in and out parameters, when I try to specify the save path, I get UnautorisedAccessException

The second way is through PdfSharp, but there I only get the part of the WebBrowser that I see on the screen, and not the whole page.

An alternative solution may be to obtain a clean Html file, after running JavaScript

  • Yes, I looked, PdfSharp turned out to be the most close, with it you can do the conversion along the path WebBrowser-> XPS-> PDF, but the problem is that in XPS it translates only the currently visible part in the browser. There are no problems with XPS-> PDF conversion - Artemiy Borodin
  • See the nuget packages for the html-to-pdf tag. Many of them say that they understand JavaScript. Try everything, maybe something will do. - Alexander Petrov

2 answers 2

There is a great service for converting HTML to PDF using C #: http://wkhtmltopdf.org

There is also a small library to speed up work: https://github.com/codaxy/wkhtmltopdf

I think you can figure it out.

    You can try to use the library for c # iTextsharp, is a port with java itext. The library has great functionality, and also supports the method of distillation from HTML to PDF.

    Here is a small piece of my code, it can help.

    html

     <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type"content="text/html;charset=utf-8"> <title></title> </head> <body> <table border="0" <!--style="width: 100%; padding: 10px 15px;"--> > <tr> <td><h2 align="center">Акционерное общество «одуванчик»</h2></td> </tr> <tr> <td> <p align="center">Тема:</p> <p align="center">[THEME]</p> </td> </tr> <tr> <td> <div align="right"> <p>Выполнил:</p> <p>[AUTHOR]</p> </div> </td> </tr> <tr> <td><p align="center">[CITY] [YEAR]г.</p></td> </tr> </table> </body> </html> 

    c #

     // Read in the contents of the html file... StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(File.ReadAllText(@"..\..\TitlePage.html")); //string contents = File.ReadAllText(@"..\..\TitlePage.html"); // Replace the placeholders with the user-specified text stringBuilder = stringBuilder.Replace("[THEME]", theme); stringBuilder = stringBuilder.Replace("[AUTHOR]", authorName); stringBuilder = stringBuilder.Replace("[CITY]", plase); stringBuilder = stringBuilder.Replace("[YEAR]", year); // Step 4: Parse the HTML string into a collection of elements... //var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null); //Path to our font string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); //Register the font with iTextSharp iTextSharp.text.FontFactory.Register(arialuniTff); //Create a new stylesheet iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet(); //Set the default body font to our registered font's internal name ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS"); //Set the default encoding to support Unicode characters ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST); foreach (var element in list) { doc.Add(element); } 

    The library itself https://sourceforge.net/projects/itextsharp/

    • JavaScript does she understand? C # does not know the contents of the html file - Artemiy Borodin
    • As far as I know, the library is able to overtake html from css. - Naomiss