This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Directly convert .aspx to .pdf
I have made one aspx page and generated chart on it through chart control of asp.net.
But i am not able to generate this generated chart to pdf document. i want to make my aspx page as pdf document. Thank you in advance for support.
When you try to convert the aspx page to pdf that means inturn you are going to convert the rendered html to pdf so here is a good link that may help you though i did not tried it but looks promising
It's Here
NB:I am assuming that your page does not include any flash charting.
use itextsharp.dll I m sure you can get ur page in pdf using this dll
very easy to implement.
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
// This will be on button's click
string attachment = "attachment; filename=" + filename+ ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "7pt");
htextw.AddStyleAttribute("color", "Black");
Panel_Name.RenderControl(htextw);//Name of the Panel
Document document = new Document();
document = new Document(PageSize.A4, 5, 5, 15, 5);
FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
Paste this some where on page
public override void VerifyRenderingInServerForm(Control control)
{
}
Related
i am developing an app in which i want to give support of export data in to pdf if user want then he can get the grid data into pdf .
i have tried many methods but not working.
its working by using itextsharp third party dll but not directly .
Using :c# ,asp.net
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Pdf stands for portable document format. Since it is a standard format you will have to put your data in that format only. other wise it will be a corrupt file and wont be prased by pdf readers. You will have to use 3rd party tools or roll your own.
Writing a decent PDF parser is a huge amount of work (we are talking years) so most people find it far more cost-effective so license IText or a similar library.
you can do it by using isharptext if you want to do it with code than you'll have to write parser .
I am using jquery ajax to call function from webservice.
In that function I am creating a pdf file using itextsharp tool.
I want that my pdf file created should open in browser when return.
can anyone help me what should be my return type for that
Below is the code I am using in webservice
public void GeneratePDf(string ID) {
string attachment = "attachment; filename=" + ID + ".pdf";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
htextw.AddStyleAttribute("font-size", "12px");
htextw.AddStyleAttribute("color", "Black");
Page pg = new Page();
HtmlForm frm = new HtmlForm();
pg.EnableEventValidation = false;
pg.RenderControl(htextw);
Document document = new Document();
document = new Document(PageSize.A4, 10, 10, 0, 0);
PdfWriter.GetInstance(document, HttpContext.Current.Response.OutputStream);
document.Open();
Font verdana = FontFactory.GetFont("Verdana", 10, Font.BOLD, new CMYKColor(75, 68, 67, 90));
PdfPCell blank1 = new PdfPCell(new Phrase("Hello ", verdana));
document.Add(blank1);
//document.Add(tablegrid);
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
HttpContext.Current.Response.Write(document);
}
Can anyone tell me what I am doing wrong
The short answer is, "don't use AJAX for this", you are creating unnecessary complication. Instead, just make a normal GET/POST request via your browser. You can still use JavaScript if you want but the important part is that you have the browser make the request so that it can receive the response.
The long answer is...
Web servers respond to requests from web browsers and things happen just as you expect them to (usually). Web browsers have a list of content types that they are aware and use this list to sometimes parse the server's response and sometimes hand it off to a 3rd party application. Once you start messing around with AJAX and other similar technologies you break this model and are saying that you want to handle the processing instead of the browser. The browser will broker your request and the server's response but otherwise it won't do anything unless you tell it to. This works great for string-like things but gets much more complicated when you deal with binary data.
I am using the below code to export gridview to PDF
form1.Controls.Clear();
form1.Controls.Add(GridView1);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
string html = "<html><body>" + sw.ToString() + "</body></html>";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.AddAuthor("Ram");
document.AddSubject("Export To pdf");
document.Open();
string tempFile = Path.GetTempFileName();
using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
{
tempwriter.Write(html);
}
HtmlParser.Parse(document, tempFile);
document.Close();
writer.Close();
File.Delete(tempFile);
writer = null;
document = null;
Response.End();
I have checked that grridview has 10 rows by putting breakpoint. But I am getting error at
document.Close();
that
The document has no pages.
Any suggestion how to fix it?
1) Setting a breakpoint to see that your grid view has 10 rows helps, but only checks part of the problem. You also need to check the contents of tempFile. That's what iText is actually working with. If it's empty, you'll sill get the "doc has no pages" exception.
2.1) HtmlParser no longer exists within iText. Having said that, I just dug up this code sample via google:
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("html1.pdf"));
HtmlParser.parse(document, "example.html");
}
No opens or closes, just a call to HtmlParser. It's quite possible that HtmlParser checks to see if the document is already open, and won't proceed if it is... that would explain the behavior you're seeing.
2.2) The "correct" way to convert HTML these days goes something like this:
String html = readHtml();
List<Element> objects = HTMLWorker.parseToList(new StringReader(html), null);
for (Element element : objects)
document.add(element);
I had same issue and the below suggestion help and my issue is solved
Document has no pages means your GridView data is lost when you export.
Hence in the Export button event rebind the GridView with data from database
https://www.aspforums.net/Threads/264988/Export-GridView-to-PDF-Error-Document-has-no-pages/
How to export my aspx page (include buttons and Grids) to PDF?
Searching the web I found iTextSharp, but it works only with normal html.
If my page has Grids or Buttons, these do not appear in PDF.
My current code for export to PDF.
This code exports only basic html (no buttons or grids).
string attachment = "attachment; filename=AllPage.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(htextw);
Document document = new Document();
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
StringReader str = new StringReader(stw.ToString());
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.Parse(str);
document.Close();
Response.Write(document);
Response.End();
Just use wkhtmltopdf. It'll handle anything short of an ActiveX control.
What's the easiest way to convert a BMP file to a single page PDF using ASP.net? I'm going to generate the 8.5" x 11" BMP in Flash (which means I can manipulate it to make it as easy as possible), then use a POST to upload it to an ASP page, which will convert it to a PDF and redirect the user to the PDF. I don't want to add any margins or anything else, it will be laid out properly in the BMP for full-bleed.
Would it be easier to convert it to PDF in Flash, then upload?
Thanks!
You can use iTextSharp to create a PDF and insert the image into the document. This can be done all in memory with a final PDF produced to client.
The following is an MVC method, stripped for display, but should see how to do this.
[HttpGet]
public FileStreamResult Export(int? ID)
{
MemoryStream stream = new MemoryStream();
Document pdf = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
pdf.Open();
PdfPTable tblImage = new PdfPTable(1);
tblImage.AddCell(Image.GetInstance(LogChart())); //The LogChart method returns image
pdf.Add(Image);
pdf.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Log.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
}