In the Venus sourcing system, a new export PDF function is added. The entire feature uses third-party software wkhtmltopdf (download) official website https://wkhtmltopdf.org/offers more version download
He can convert HTML page to PDF, the software is simply incredible-incredible, the function is too powerful.
Because, I have a HTML, reference a lot of CSS, and the page is basically JS dynamically generated, have been worried that the wkhtmltopdf generated PDF will be a blank
Not quite, after the conversion, those CSS and JS are not "distorted".
Here's the code that uses C # to generate the HTML PDF:
stringURL ="http://www.dotnetcms.org/About.aspx";
string pdf = "c:\pdf\bin\wkhtmltopdf.exe"
stringfilename =Guid.NewGuid (). ToString (); stringPdfpath = filename +". pdf"; Process P= System.Diagnostics.Process.Start (pdf, URL +" \""+ Server.MapPath (Pdfpath) +"\""); p.WaitForExit (); //Method 1, use the following code, open online//Response.Redirect (Pdfpath); //Method 2, use the following code to let the customer downloadFileStream fs =NewFileStream (Server.MapPath (Pdfpath), FileMode.Open); byte[] File =New byte[FS. Length]; Fs. Read (file,0, file. Length); Fs. Close (); Response.Clear (); Response.AddHeader ("content-disposition","attachment; Filename="+ filename +". pdf");//forced download in binary streaming modeResponse.ContentType ="Application/octet-stream"; Response.BinaryWrite (file);
In the above code, the URL is the page to be passed, the PDF parameter is wkhtmltopdf.exe for the path you actually installed.
Of course, in the real world, if you use IIS and want to generate PDFs via ASP, you need to be aware of the permissions, first, find the application pool used by the application, click Advanced on the application pool, have a "logo", Modify the default applicationpoolidentity to LocalSystem. Otherwise, it may fail to invoke EXE because of insufficient permissions.
ASP. NET C # export PDF based on HTML page