Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to convert html to pdf using google api wkhtmltopdf in asp.net.
i want to convert a string instead of a web page. can somebody tells me how to do it. here is the code.
C#
string url = HttpContext.Current.Server.MapPath("HTMLPage.htm");
//this will be the output path of pfd file
string filepath = HttpContext.Current.Server.MapPath("PDF/saif.pdf");

//variable to store pdf file content
byte[] fileContent = null;

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

//set the executable location
process.StartInfo.FileName = HttpContext.Current.Server.MapPath("PDFConverter/wkhtmltopdf.exe");

//set the arguments to the exectuable
// wkhtmltopdf [OPTIONS]... <input fileContent> [More input fileContents] <output filecontent="">

process.StartInfo.Arguments = url + " "  + filepath;
// process.StartInfo.Arguments = "this is the test"+""+filepath;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
//run the executable
process.Start();

//wait until the conversion is done
process.WaitForExit();

// read the exit code, close process    
int returnCode = process.ExitCode;
process.Close();

//initialize the filestream with filepath
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
fileContent = new byte[(int)fs.Length];

//read the content
fs.Read(fileContent, 0, (int)fs.Length);

//close the stream
fs.Close();

return fileContent;

i want to insert a string in url instead of htmlpage.html. how to do it.
thanks in advance.
Posted
v2
Comments
sri senthil kumar 8-May-13 8:40am    
If you want to convert a string you have given into a pdf doc, Why dont you use PDFSharf/MigraDoc it will be much easier when you want to set the UI perfectly and load content/UI dynamically?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900