Print aspx page to PDF on button click, but print PDF serverside - c#

I have a report that's displayed on a webpage, and the users have been printing this webpage to PDF. What I need is for the webpage to automatically print to PDF (save) to a location on the websites server when user clicks a button.
iTextSharp doesn't seem to let me just save the PDF exactly how it looks when you Print To PDF (keeping css etc), also there's some licencing complications. PrinceXML looks good but is way too expensive.
Is there no way to open a new aspx page and then print it to PDF server side?

You can use the built-in RLDC report. It's free, has a nice design and features, and allows you to print to PDF. You don't need to save a PDF file and worry about managing and deleting it, simply print it to a stream and use the stream to download.
Something like this:
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding,
out extension, out streamids, out warnings);
And then write bytes to the download stream.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename= filename" + "." + extension);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.Flush();
Response.End();

Related

How to force view in browser of pdf and word docs

I am required to open various files types such as pdf and word in browser I see that the most accepted solution seems to be the simlar as to what I have done
public string GetDocument(Guid UserId, string Filename)
{
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
return File(doc, mimeType);
}
My quesiton is how do i turn my raw file on the server to something that can be served to the browser I persume I have to change it to byte if so how would one change a pdf to bytes. And for that matter word files
You can use something like:
public void GetDocument(Guid UserId, string Filename)
{
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
Response.WriteFile(fileName);
Response.End();
}
Note, Response.WriteFile expects the FULL path to the file, so if the filename variable isn't the full path, you'll need to map it.
In the case of the PDF, most browsers will open in inside the browser, for other file types your mileage may vary, depending on whether the browser has the capability to display that file type inline.
You cannot force it from the server end.
Some browsers (such as Chrome) can render a PDF just fine, but a PDF rendered from a server will only open in Chrome only if the .pdf file extension is associated with Chrome. You can't change the associations without access to the machine on which the browser is running.
As far as I know, you cannot open a Word doc directly in any browser. You have to use a viewer.
Using a viewer you can display PDF or Word docs within the page-- this is different from downloading the file and displaying it natively, but it may suit your purpose.
A few viewers are freely available. Here's two.
Google docs viewer
Office Web Apps viewer

PDF won't display in browser window

I am using a handler to get and display a PDF in the browser window using the code below:
byte[] byt = RetrieveDocument(int.Parse(context.Request.Params["id"]), context.Request.Params["title"]);
string file = WriteDocumentFilePDF(byt);
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-length", byt.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=programdetails.pdf");
HttpContext.Current.Response.BinaryWrite(byt);
HttpContext.Current.Response.End();
The function WriteDocumentFilePDF successfully writes the PDF to the temp directory. I have the above code working correctly in a different application. Am I missing something?
When debugging issues like this, I find that Fiddler is an invaluable tool; many many times it has saved me from simple mistakes. Also, this site http://www.c-sharpcorner.com/uploadfile/prathore/what-is-an-ashx-file-handler-or-web-handler/ gives an example of doing the same thing but with a GIF image. The difference between your example and his seems to be the use of Response.WriteFile() rather than the direct write to the Response using BinaryWrite().
I would perform a Response.ClearHeaders() before I set the content-type and I would remove the call to Response.End().
Does it make a difference if you pass the byte[] to memorystream first? So something like
byte[] byt = RetrieveDocument(int.Parse(context.Request.Params["id"]), context.Request.Params["title"]);
string file = WriteDocumentFilePDF(byt);
MemoryStream ms = new MemoryStream(byt);
And then add your headers
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=programdetails.pdf");
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();

How to print RDLC Report in Firefox

I want to print my RDLC report in Firefox but the Print button icon is not displaying in Firefox browser but It works fine in IE.
Can anyone guide me how to print my report in Firefox browser.
Unfortunately there is no way to print an rdlc report in firefox.
Rdlc print is only works in ie, due to the activeX requirements.
More info here:Configuring and Using the ReportViewer Toolbar
There is an article on msdn about how print a local report without preview, but its only works with winforms report viewer. In asp.net you cannot acces to the client's printer.
Walkthrough: Printing a Local Report without Preview
Instead of print in firefox, you can export rdlc directly.
For an example:
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
string filename = "YourFileName";
// Setup the report viewer object and get the array of bytes
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "YourReportHere.rdlc";
byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download

opening a file in the browser instead of downloading it

Im using ssrs through a reports server to generate a resultStream byte array using ReportExecutionService.Render() which I am currently serving to the user with the following code. Is there a way I can use this same byte array to automatically open the report in a new browser window instead of going to the save/open dialog?
public void RenderReport (byte[] reportDigits, ReportItem reportItem)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = reportItem.ReportMimeType;
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportItem.ExportName));
response.OutputStream.Write(reportDigits, 0, reportDigits.Length);
response.End();
}
In the past I have used a separate ReportViewer.aspx page that I would open first then display the report but would like to do it all in code behind if that is possible.
Thanks
It's this line:
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportItem.ExportName));
Thats causing it to be downloaded. Comment out that line, and as long as the browser can handle the mime type, it will render in the browser window.
Simply change the Header that you are adding to something other than an attachement. Make it the format of your data--and hopefully the browser will recognize it.

View PDF in IE6

I am trying to open a PDF document to display within IE6. I am using the following snippet:
response.ContentType = healthMedia.MediaKey.MimeType;
response.ClearHeaders();
response.AddHeader("Content-Disposition", "inline; filename=" + mediaKeyId);
int contentLength = healthMedia.Content.Length;
response.AppendHeader("content-length", Convert.ToString(contentLength));
response.OutputStream.Write(healthMedia.Content, 0, contentLength);
healthMedia.MediaKey.MimeType; is equal to 'application/pdf'
This brings up the Save dialog. If I comment out Response.ClearHeaders(); I get a new window to popup but it's contents is a bunch of jibberish (random encoding text).
How can I get IE6 to open the PDF correctly?
-Nick
Have you tried Response.End() and also Response.Buffer = true? You may also need to set a caching policy.
In case it helps, here's a method I've used before to render in-browser PDFs...
Use response.BinaryWrite() instead of response.OutputStream.Write()

Categories

Resources