TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
515
46k
About converting Html content to PDF format
Jul 21 2020 10:56 AM
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(
"<head></head><body style='color:red;'>My HTML Layout</body>"
);
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.
Reply
Answers (
4
)
How to save values in asp.net mvc when using checkbox and dropdownlist
Razor pages single field SQL Server update