convert html to pdf

  • Thread starter Thread starter suji
  • Start date Start date
S

suji

Hai,

Are there any 3rd party controls who provide free libraries to convert
my html form to pdf in c#?
I need to have a button on my form, that should convert my page to pdf
format. Can any one help me to write code for that in c# with
libraries?

regards,
Suji.
 
if your form is not filled with complicated controls you can use ghtmldoc.exe.


Step 1:

Google ghtmldoc.exe and get licensed copy or use the olde version. Once you got the file

Step2 :

Create the function in your form say like GeneratePDF()
string sFileName = "test.html";

string sPage = Server.MapPath(@"~\common\PDFHelper\"+sFileName + ".html"); //Replace ~\common\PDFHelper\ with your directory of choice


//Checkbox's are converted by this tool to pfd , so we are going to replace all the checkboxes with

//literal control with text [ ]

ReplaceCheckBoxControls(page.Controls);


StreamWriter sWriter = File.CreateText(sPage);

System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sWriter);

page.RenderControl(htw);

htw.Close();


sWriter.Close();


System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

pProcess.StartInfo.FileName = Server.MapPath(@"~\common\PDFHelper\ghtmldoc.exe");

pProcess.StartInfo.Arguments = "--webpage --quiet " + sFontSize + m_sWaterMark + " --bodyfont verdana " + sLandScape + " -t pdf14 -f " + sFileName + ".pdf " + sFileName + ".html";

pProcess.StartInfo.WorkingDirectory = Server.MapPath(@"~\common\PDFHelper");


pProcess.Start();



2a.Here is ReplaceCheckbox control function to help you out

private void ReplaceCheckBoxControls(ControlCollection controls)

{

for (int i=controls.Count -1; i>=0; i--)

{

if(controls.HasControls())

ReplaceCheckBoxControls(controls.Controls);

else if(controls is CheckBox)

{

ReplaceCheckBoxControls(controls as CheckBox);

}

else if(controls is Label)

{

Label lbl = controls as Label;

if(lbl.Text == string.Empty)

lbl.Text = "  ";

}

}


}

private void ReplaceCheckBoxControls(CheckBox chkBox)

{

if(chkBox.Parent != null)

{

Control parent = chkBox.Parent;

int idx = parent.Controls.IndexOf(chkBox);

parent.Controls.RemoveAt(idx);

LiteralControl literal = new LiteralControl();

parent.Controls.AddAt(idx,literal);

literal.Text = "[ ] "+ chkBox.Text;


}


}

3.After the above step forward your request to the new aspx page name it something like DispalyPDf or ViewPDF , on the page load sleep for 1000 miliseconds and

check for the pdf file you created using ghtmdoc.exe, if it's avialbale render that file to the browser.



I hope this helps.
 
Hello suji,

Why to use 3rd part tools if you can convert it manually using XSL-FO
see manual there http://www.antennahouse.com/XSLsample/XSLsample.htm

s> Hai,
s>
s> Are there any 3rd party controls who provide free libraries to
s> convert
s> my html form to pdf in c#?
s> I need to have a button on my form, that should convert my page to
s> pdf
s> format. Can any one help me to write code for that in c# with
s> libraries?
s> regards,
s> Suji.
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Back
Top