简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM