Generate HTML PDF

Source: Internet
Author: User

Generally, you can generate common features that do not require user and password verification. The following code is used to generate a PDF file for the pages based on Windows authentication (my work is based on SharePoint)

The Code is as follows:

Public bool htmltopdf (string URL, string path) {try {string filename = pdffolder + "\" + path + ". PDF "; process P = new system. diagnostics. process (); p. startinfo. filename = @ "C: \ pdffolder \ wkhtmltow..exe"; // P. startinfo. username = "Administrator"; // user name // P. startinfo. password = stringtosecurestring ("password01! "); // User Password // P. startinfo. arguments = "" + "\" "+ URL +" \ "" + "" + "\" "+ filename +" \ ""; p. startinfo. arguments = string. format ("\" {0} \ "\" {1} \ "-- password \" {2} \ "-- USERNAME \" {3} \ "", URL, filename, "[page Password]", "[user name for page logon]"); p. startinfo. useshellexecute = false; // needs to be false in order to redirect output p. startinfo. redirectstandardoutput = true; p. startinfo. redirectstandarderror = true; p. startinfo. redirectstandardinput = true; // redirect all 3, as it shoshould be all 3 or none p. startinfo. workingdirectory = "C: \ pdffolder"; p. start (); // read the output here... string output = P. standardoutput. readtoend ();//... then wait n milliseconds for exit (as after exit, it can't read the output) p. waitforexit (60000); // read the exit code, close process int returncode = P. exitcode; p. close (); // If 0 or 2, it worked (not sure about other values, I want a better way to confirm this) return (returncode = 0 | returncode = 2);} catch (exception ex) {} return false ;}
Public static securestring stringtosecurestring (string Str)
{
Securestring securestr = new securestring ();
Char [] chars = Str. tochararray ();
For (INT I = 0; I <chars. length; I ++)
{
Securestr. appendchar (chars [I]);
}
Return securestr;
}


The usage is as follows:

HtmlToPdf("http://www.baidu.com", "buidu1")

But there may be garbled issues, there may be two ways, first, in the code modification, but now because the project has no requirements, there is no research for the moment, the second, if it is your own page, you can modify the encoding method of the page.

 

You can download the wkhtmltocmd.exe file from the following network:

Http://files.cnblogs.com/gzh4455/wkhtmltopdf.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.