Home Contact

Brian Sherwin's Blog

Moving at the Speed of .Net

News

Tag Cloud


Archives

Links

Syndication:

Generating a PDF from Reporting Services without the Report Viewer

This post is primarily for Steve since he has been bugging me about it.  (Forgive the VB, but this is what I did the demos in).  Steve doesn't like doing SQLRS, but personally, I'd like to do more of it.  I just can't seem to come up with any clients that want to do it.

Any way, here is the process of creating the PDF without using the Report Viewer.

Create a reference to the Reporting Services Web Services then get a variable reference for it:

Dim ws As New SQLRS.ReportingService
Dim creds As New System.Net.NetworkCredential

Next set the authentication for the web service:

creds.UserName = "RSUser"
creds.Password = "SomePassword"
ws.Credentials = creds

Finally, set some of the web service variables for how you want the data rendered.  You don't have to set all of these, but you will have to pass them all in, so I just like to be clear about what I am doing:

Dim result() As Byte
Dim report As String = "/Sample Reports/Sales Order Detail"
Dim format As String = "PDF"
Dim historyid As String = Nothing
Dim devinfo As String = ""
Dim credentials() As SQLRS.DataSourceCredentials = Nothing
Dim showhide As String = Nothing
Dim encoding As String
Dim mimetype As String
Dim warnings() As SQLRS.Warning = Nothing
Dim reporthistoryparams As SQLRS.ParameterValue() = Nothing
Dim streamid() As String = Nothing
Dim sh As New SQLRS.SessionHeader
ws.SessionHeaderValue = sh

Next, get the result from the web service:

result = ws.Render(report, format, historyid, devinfo, parameters, _
            credentials, showhide, encoding, mimetype, reporthistoryparams, _
            warnings, streamid)

And stuff it into the workflow (in this case, open the PDF in your favorite browser):

Response.ClearContent()
Response.ContentType = "application/pdf"
Response.BinaryWrite(result)
Response.Flush()

And for the record Steve...No, I will not convert this to C# for you, and I feel no shame to post VB code.


Feedback

# re: Generating a PDF from Reporting Services without the Report Viewer

No shame in VB!! Amen! 4/30/2007 12:24 AM | Tim Hibbard

# re: Generating a PDF from Reporting Services without the Report Viewer

Well, seeing as you did do me a favor by posting the example code - I suppose I can't complain (too loudly) about it being in VB. :)

Thanks Brian! 4/30/2007 11:17 AM | Steven Harman

# Links (4/30/2007)

.NET Generating a PDF from Reporting Services without the Report Viewer How many ways to do a String 4/30/2007 9:08 PM | Member Blogs

# re: Generating a PDF from Reporting Services without the Report Viewer

If I need to merge 2 pdf files generated as above, what is the way to go? 6/22/2007 3:17 PM | Aashish

# re: Generating a PDF from Reporting Services without the Report Viewer

Hi, i am trying to use this sample code but the parameters param in the signature of the render method does not accepts arrays, only one param at a time, what if i need to pass more than one, like 3.

Thanks,

shauki 7/26/2007 4:59 PM | shauki

# re: Generating a PDF from Reporting Services without the Report Viewer

This is for the c# developers


byte[] result;
string report = "/Sample Reports/Sales Order Detail";
string format = "PDF";
string historyid = null;
string devinfo = "";
SQLRS.DataSourceCredentials[] credentials = null;
string showhide = null;
string encoding;
string mimetype;
SQLRS.Warning[] warnings = null;
SQLRS.ParameterValue[] reporthistoryparams = null;
string[] streamid = null;
SQLRS.SessionHeader sh = new SQLRS.SessionHeader();
ws.SessionHeaderValue = sh;

result = ws.Render(report, format, historyid, devinfo, parameters, credentials, showhide, encoding, mimetype, reporthistoryparams,
warnings, streamid);

Response.ClearContent();
Response.ContentType = "application/pdf";
Response.BinaryWrite(result);
Response.Flush(); 7/30/2007 11:33 AM | Netunes

# re: Generating a PDF from Reporting Services without the Report Viewer

Hi, I having some trouble when generating the pdf, the browser returns the following message:

The file is damaged and could not be repaired.

Does anyone know why by any chance???


thanks, 9/4/2007 4:31 PM | Mateus

# re: Generating a PDF from Reporting Services without the Report Viewer

I use this method; unfortunately it times out on me because I have many images I'm trying to load. Do you know of a way to write code to resize the images first before rendering the report?

Any suggestions would be appreciated!
Thanks! 9/11/2007 2:38 PM | G-Lo

# re: Generating a PDF from Reporting Services without the Report Viewer

can someone give further clarification on SQLRS 2/6/2008 8:39 AM | Lost

# re: Generating a PDF from Reporting Services without the Report Viewer

Simply awsome posting! 6/25/2008 4:30 PM | Shanker Kana

# re: Generating a PDF from Reporting Services without the Report Viewer

Is this possible within 2008? It appears I can reference to the Reporting Services Web Services, but unable to set a variable reference. 8/24/2008 10:14 PM | Michael

# re: Generating a PDF from Reporting Services without the Report Viewer

Can this be done without using a Reporting Service Web Service? My reports are generated via .NET VS 2005. 11/18/2008 11:03 AM | Ron

# re: Generating a PDF from Reporting Services without the Report Viewer

The PDF rendering in SQL Report Services 2008 is vastly improved, and can support rendering of HTML text in the PDF report (which is a very useful feature in itself).

Unfortunatly, the PDFs produced aren't standards compatible and some readers are unable to open them. So any post-render processing you're doing, such as merging a PDF report with a letterhead/watermark as mentioned in your other blog post "Merge PDFs using ITextSharp", the process won't work anymore!

PDFSharp and iTextSharp cannot read PDFs that have come out of SQLRS2008; shame really. 2/7/2009 1:37 PM | Sam

# re: Generating a PDF from Reporting Services without the Report Viewer

Hi,

[PDFSharp and iTextSharp cannot read PDFs that have come out of SQLRS2008]

Is there any solution/workaround to merge SQLRS2008 PDFs using iTextSharp

Thanks. 2/12/2009 6:04 PM | Riaz Afridi

# re: Generating a PDF from Reporting Services without the Report Viewer

Riaz,

I've tried everything I can to get SQLRS2008 PDFs to work in PDFSharp and iTextSharp since I upgraded to 2008 the other month, and still no joy.

I believe the PDF renderer inserts a invalid character in the data stream that Adobe Reader and that can deal with, but that the .net librarys cannot. They stumble because they don't read the entire stream correctly, the checksum then doesn't match the one in file, and so the file fails because the library thinks it hasn't decompressed the PDF properly, or think that it's encrypted.

In the mean time, I am going to have to get another box running SQLRS2005 and run the reports of the 2008 database, and not use any new RS2008 features.

Microsoft never make things easy nowadays, a known installer bug made my simple 2005>2008 upgrade a nightmare, if they knew about it then why not fix the installers? 2/27/2009 4:16 PM | Sam

# re: Generating a PDF from Reporting Services without the Report Viewer

If you download the source code for iTextSharp and recompile the dll. It will fix the problem with the blank sheet being exported. 3/9/2009 2:56 PM | Brian

# re: Generating a PDF from Reporting Services without the Report Viewer

Sam,

I'm running into a similar error with PDF format using SSRS 2005. Interstingly enough, there are no issues when opening the PDF using Adobe Acrobat 8.0 Pro or the free Acrobat 9.0 reader. I tested it with an iPhone and there were no issues either. However, half the users in my office run Acrobat Standard 7.0 and they are getting the error.

Here's another thing: If we render the report inside a Report Viewer control, and then export to PDF from the control, there are no issues...

Any thoughts? 3/10/2009 11:22 AM | SG

# re: Generating a PDF from Reporting Services without the Report Viewer

Problem Solved!

I went through some other posts and found the following:

Open your PDF file with notepad and take a look at the last line. In my particular case, the HTML from my page was being appended by the Response to the PDF file and was casuing the error. I suppose Adobe worked the kinks out in version 8 and 9, but never patched 7.

To get around this, I needed to add Response.End() after Response.BinaryWrite()

This fixed my issue, hopefully this will also work for SSRS 2008 and the other PDF viewers.

SG 3/10/2009 11:52 AM | SG

# re: Generating a PDF from Reporting Services without the Report Viewer

Hi,
I can't use above code in 2008 to generate the pdf because I didn't see "SessionHeader" class in Reporting Service websvr.

Anyone please tell me how to do in 2008.

Thanks 5/5/2009 4:04 AM | Si Thu

# re: Generating a PDF from Reporting Services without the Report Viewer

I'm having that same problem with iTextSharp and PDF's exported from RS, very frustrating.

Brian, why do you say that downloading the source code and recompiling will fix it (3/9/09), aren't the DLL's on the website built from the same source code? I have version 4.1.2.

Is there a specific change you made to the source code to get it to work? 6/4/2009 3:20 PM | Erik Andersen

Post a comment