export .aspx content to pdf


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

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.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.word";

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



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?  

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


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);


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).
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.

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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?
dchau12Author Commented:
What is the difference?
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).
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.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That's what I was thinking, but I have never tried this.
dchau12Author Commented:
Thanks Gavin.  That confirms for me what I tested.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.