I am writing a web service in C# using NReco.PdfConverter
and wkhtml
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 $.ajax()
.
Using NReco.PdfConverter
, 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"
};
converter.GeneratePdf(htmlContent);
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} ",
FormsAuthentication.FormsCookieName,
Request.Cookies[FormsAuthentication.FormsCookieName] );
pdfGen.GeneratePdfFromFile("your_sharepoint_web_page_url", null, "output.pdf");
--- UPDATE ---
for HTTP Basic auth:
pdfGen.CustomWkHtmlArgs = String.Format(" --username {0} --password {1}", username, pwd );