Hello Everyone,
Hope everyone is fine.
I have HTML content and want to convert that to pdf format and save it to a local project folder.
I have installed HtmlRender.PdfSharp NuGet library in my project.
I have also tried a few examples but did not work out. Even I did not find any error in that code.
So I am not getting, what I am doing wrong.
I have tried the following code :
- protected void buttonClick(Object sender, EventArgs e)
- {
- Byte[] fileContent=PdfSharpConvert("My HTML Layout");
- string fileName = "New.pdf";
- string[] stringParts = fileName.Split(new char[] { '.' });
- string strType = stringParts[1];
- Response.Clear();
- Response.ClearContent();
- Response.ClearHeaders();
- Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
- //Set the content type as file extension type
- Response.ContentType = strType;
- //Write the file content
- this.Response.BinaryWrite(fileContent);
- this.Response.End();
- }
- public static Byte[] PdfSharpConvert(String html)
- {
- Byte[] res = null;
- using (MemoryStream ms = new MemoryStream())
- {
- var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
- pdf.Save(ms);
- res = ms.ToArray();
- }
- return res;
- }
Please let me know anything can be done to fix these.
Thank you in advance.
Marvin ReidPosted Jul 6, 2021, 9:07 PM
Guest UserPosted Jul 22, 2020, 7:54 AM
Leon DPosted Jul 21, 2020, 8:07 PM
Farhan AhmedPosted Jul 21, 2020, 11:34 AM