Geeks With Blogs

@bsherwin

Brian Sherwin's Blog Moving at the Speed of .Net

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.

Posted on Sunday, April 29, 2007 10:04 PM | Back to top


Comments on this post: Generating a PDF from Reporting Services without the Report Viewer

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
No shame in VB!! Amen!
Left by Tim Hibbard on Apr 30, 2007 12:24 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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!
Left by Steven Harman on Apr 30, 2007 11:17 AM

# Links (4/30/2007)
Requesting Gravatar...
.NET Generating a PDF from Reporting Services without the Report Viewer How many ways to do a String
Left by Member Blogs on Apr 30, 2007 9:08 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
If I need to merge 2 pdf files generated as above, what is the way to go?
Left by Aashish on Jun 22, 2007 3:17 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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
Left by shauki on Jul 26, 2007 4:59 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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();
Left by Netunes on Jul 30, 2007 11:33 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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,
Left by Mateus on Sep 04, 2007 4:31 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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!
Left by G-Lo on Sep 11, 2007 2:38 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
can someone give further clarification on SQLRS
Left by Lost on Feb 06, 2008 8:39 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
Simply awsome posting!
Left by Shanker Kana on Jun 25, 2008 4:30 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
Is this possible within 2008? It appears I can reference to the Reporting Services Web Services, but unable to set a variable reference.
Left by Michael on Aug 24, 2008 10:14 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
Can this be done without using a Reporting Service Web Service? My reports are generated via .NET VS 2005.
Left by Ron on Nov 18, 2008 11:03 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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.
Left by Sam on Feb 07, 2009 1:37 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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.
Left by Riaz Afridi on Feb 12, 2009 6:04 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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?
Left by Sam on Feb 27, 2009 4:16 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
If you download the source code for iTextSharp and recompile the dll. It will fix the problem with the blank sheet being exported.
Left by Brian on Mar 09, 2009 2:56 PM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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?
Left by SG on Mar 10, 2009 11:22 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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
Left by SG on Mar 10, 2009 11:52 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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
Left by Si Thu on May 05, 2009 4:04 AM

# re: Generating a PDF from Reporting Services without the Report Viewer
Requesting Gravatar...
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?
Left by Erik Andersen on Jun 04, 2009 3:20 PM

Your comment:
 (will show your gravatar)


Copyright © Brian Sherwin | Powered by: GeeksWithBlogs.net | Join free