• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1834
  • Last Modified:

export .aspx content to pdf

Hello,

My boss bought access to this site back in February.  This is my first time using it.

I read this article on this site about exporting .aspx context to pdf.  The contents of this article are between the rows of asterics:

*************************************************************************************
I need to export a report (ASP.Net page) to PDF and Word. Is there a way to do this without using Crystal Reports? I do not have that option. Yhanks.
 
 Send to a Friend     Printer Friendly   See Solution  
   
 Accepted Answer from bglodde
Date: 11/26/2005 01:28PM CST
Grade: A
 Accepted Answer  

From ASP.NET to PDF, you can use iText.NET
http://sourceforge.net/projects/itextdotnet/

To export to MSWord, as long as the client has it installed, something similar to this will work:
' export a datagrid to ms word
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();

I'm sure there are simpler commercial solutions out there as well.

***********************************************************************************************

I tried to impliment this in my code for a pdf file by changing the line  Response.ContentType = "application/pdf"
I then changed the extension of the file name to .pdf.

I created a simple asp table with a five word sentence.  It attempted to open the pdf file, but then gave an error message saying that the file content was corrupt.

Although I do not need to export the contents of my website to word, I tried it anyway.  Things like hyperlinnks, bulletlist gave immidiate errors in the code (error example: "Control 'ctl00_FlowerText_ctl03' of type 'BulletedList' must be placed inside a form tag with runat=server.").   The Dundas graphs (I am using dundas software to generate my graphics) only displayed as placeholders.  Only tables with bare text seemed to display without problems.  This is irrelevant though, because I need to get this information to a PDF format.

So, I need to find a PDF solution.  It seems like a very simple concept, but I havne't been able to make it work.  In a nutshell, I need to take the content from a .aspx web page and export it to a PDF file.  I don't need the tables or datagrids to dynamically update when the content on the site changes.  I just need the content from the site to appear in a PDF report.

What is the best way of going about this?  

0
dchau12
Asked:
dchau12
  • 5
  • 4
1 Solution
 
dchau12Author Commented:
Let me add one more detail.  All of my pages are wrapped in an encapsulating asp table.  So, in theory I should be able to send that one table to the PDF export.

Thanks,

Ryan
0
 
vladimir_kalashnikovCommented:
I have used this to create an excel file:

DataGrid dgrid = GetInfoFromDB();

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=info.xls");
Response.Charset = "";
this.EnableViewState = false;
            
System.IO.StringWriter objSW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);

dgrid.RenderControl(objHTW);
Response.Write(objSW.ToString());
Response.End();

You may be able to just change the content type to: application/pdf.  You can find a list of content types here: http://www.webmaster-toolkit.com/mime-types.shtml

I have only used this for excel files and Microsoft Word files (I just used Response.Write() to output text to the word files, you can probably do the same for pdf).
0
 
dchau12Author Commented:
That is about the exact same code that I have in the first post.  It does work for excel, and I have that functionality already built into my program.  It does not work for PDF.

Ryan
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vladimir_kalashnikovCommented:
Woah.  I must have totally spaced on that one, sorry mate, i didn't see any of that.

Have your tried using Response.Write() and manually adding text to the file that way?  Or does it not let you create a PDF file at all?
0
 
dchau12Author Commented:
What is the difference?
0
 
vladimir_kalashnikovCommented:
I mean, try and create a pdf by just writing Respose.Write("test"); instead of writing the object to the file.  I am going to guess that the .net framework (or IIS?) doesn't support outputting PDF files (i.e. it doesn't understand what to do with the PDF content type).
0
 
dchau12Author Commented:
Thanks Vladimir,

I tried a simple response.write("text test").  It works fine for excel.  It does not work for pdf.  It tries to open the pdf document and then it errors out saying that either the file type is not supported or the content is corrupt.

Ryan
0
 
GavinMannionCommented:
You cannot just create a pdf file like that. PDF is an Adobe format and you need a 3rd party control to actually create pdf documents.

Crystal Reports normally has one built in and you should be able to find a small add in on google easily enough.

excel and word files can be created by using flat html but not pdf files.
0
 
vladimir_kalashnikovCommented:
That's what I was thinking, but I have never tried this.
0
 
dchau12Author Commented:
Thanks Gavin.  That confirms for me what I tested.

Ryan
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now