K48 K48 - 25 days ago 14
C# Question

Get the entire HTML + CSS + JS of the page, and send it to a web service

I am writing a web service in C# using

that would convert web pages into PDF files.

The web pages (on SharePoint) require authorization and also contain a form that the user needs to fill in, therefore the web service cannot simply access the URL of that page and download it.

The JavaScript and CSS files are also important for the correct rendering of the form, including dozens of JS files and stylesheets from SharePoint.

So far my best idea is this:

  • When user clicks "Generate PDF", JavaScript will convert the entire current page into a single string (appending the CSS files and JS files inline);

  • POST
    that string to the webservice using

  • Using
    , it is trivial to convert that string to PDF and save it into the file:

    var converter = new HtmlToPdfConverter
    Margins = new PageMargins
    Top = 0,
    Bottom = 0,
    Left = 0,
    Right = 0
    CustomWkHtmlArgs = "--print-media-type"

How would one even approach the idea of generating a single-page HTML (including the state of the checkboxes, text inside the forms etc.) in the browser?

Is it something tremendously complicated? Is anyone aware of another solution?


You can pass authorization cookie (or header) using appropriate wkhtmltopdf option, for example (if WebForms authentication is used):

var pdfGen = new HtmlToPdfConverter();
pdfGen.CustomWkHtmlArgs  = String.Format(" --cookie {0} {1} ",
  Request.Cookies[FormsAuthentication.FormsCookieName] );
pdfGen.GeneratePdfFromFile("your_sharepoint_web_page_url", null, "output.pdf");