asp.net MVC3 uses Reporting Services to generate PDF to solve web online printing __.net

Source: Internet
Author: User

Microsoft's SQL Server Reporting Services has been used for the current product to output reports to the user. But in the Web printing has been a problem, because to invoke Microsoft's Rsclient control, but the control in the browser compatibility problems, in Google or Firefox browser will certainly not be able to get the client print interface. Consulted Microsoft, as if to say that the latest version of Rsclient control can support Google or Firefox browser, but the current SQL Server2012 is not supported.

So recently saw Finereport, see Finereport Most recommended is the PDF printing, then produced a new idea, Microsoft Reporting Services is able to export PDF function, using the existing viewer control, increase the PDF online printing function , and then export the report to a temporary directory on the server, point the browser to the temporary build PDF file, and open the pdf file online by the browser. Judging from the current test results, because the current client support to open the PDF control, the effect is very good.

From the perspective of implementation, the main solution to two key issues:
1, the paper Viewer to generate PDF files.
2, periodically delete the temporary directory PDF file.

This is relatively simple, the following is the server code:

        <summary>///PDF Print online (theoretically supported IE browser and Google browser)///</summary>///<returns&gt
            ;</returns> public ActionResult printpdf () {var serverreport = new ServerReport ();
            Serverreport.reportserverurl = Engine.reportserver;
            Serverreport.reportpath = Engine.reportpath;

            Serverreport.reportservercredentials = engine.credentials;
                    if (engine.parames!= null) {foreach (var item in engine.parames) { Item.
                    Visible = false;
                Serverreport.setparameters (item);
            }//Export print PDF string mimetype;
            string encoding;
             String filenameextension;
            Warning[] warnings;

            String[] streams;
                               var rptbytes = Serverreport.render ("PDF", DeviceInfo, Out of MimeType, out of encoding, out filenameextension,

            Out streams, out warnings);

            Delete all temporary files 1 days ago Deletetemppdffile ();

            Save PDF file to disk var fileName = Savepdf2disk (rptbytes);
         Output directly on the browser to open return Content (fileName); ///<summary>///Save PDF file to disk///</summary>///<param name= "pdfcontent 
        ">pdf content </param>///<returns></returns> public string Savepdf2disk (byte[) pdfcontent)

            {Random Random = new Random ();
            Temporary files saved to disk, by date plus random number, you can delete the temporary file every time you call.
            The file name is: 6-bit date + 5-bit random number. pdf var datestring = DateTime.Now.ToString ("YyyyMMdd"); var fileName = datestring + random. Next (99999, 1000000).

            TOSTR () + ". pdf"; var Filetemppath = Server.mappAth ("~/") + "//filetemp//" + fileName;

            FileStream Writestream = new FileStream (Filetemppath, FileMode.Create, FileAccess.Write);
            MemoryStream readstream = new MemoryStream (pdfcontent);
            int Length = 256;
            byte[] buffer = new Byte[length];
            int bytesread = readstream.read (buffer, 0, Length); Write the required bytes while (Bytesread > 0) {writestream.write (buffer, 0
                , bytesread);
            Bytesread = readstream.read (buffer, 0, Length);
            } readstream.close ();
            Writestream.close ();
        return fileName; ///<summary>///Delete temporary PDF file///</summary>///<returns></returns&
        Gt
            public void Deletetemppdffile () {var filetemppath = Server.MapPath ("~/") + "//filetemp//";

   string[] files = Directory.GetFiles (Filetemppath, "*.pdf");         int datestring = Int.

            Parse (DateTime.Now.ToString ("YyyyMMdd"));

            FileInfo fi;

            File name: 6-bit date, delete//delete all temporary files 1 days ago If the date is greater than the temporary file date.
                foreach (var file in files) {fi = new FileInfo (file); if (FI. name.length>8) {var filedatename = fi.
                    Name.substring (0, 8); try {if (datestring > int. Parse (Filedatename)) {fi.
                        Delete ();

            } ' catch {}} ' }
        }

Next is the client code, just a few JS scripts.

"'

$ (document). Ready (function () {printpdf ();}); function Printpdf () {var aj = $.ajax ({url: "/common/report/printpdf",//Jump to action data: {C Ode:reportcode}, type: ' Post ', Cache:false, DataType: ' Text ', success:
                function (data) {var pdfurl = "/filetemp/" + data;
            Showreport (Pdfurl); }, Error:function () {Alert ("Exception").
            ");

  }
        });
      function Showreport (URL) {window.location.href = URL;

  $ ("#ReportFrame"). attr ("src", url); } <div id= "Dvmask" style= "Position:absolute; z-index:999999; top:0px;
    left:0px; width:100%; Height:expression (Document.body.clientHeight);
    min-height:100%; Background-color: #ddd; Text-align:center; opacity:0.8; Filter:alpha (opacity=80); " > <div style= "position:relative; top:50px; Filter:alpha (opacity=100);
        Opacity:1; color:red; re>T-align:center; "
        >  <br/> is loading PDF data, please wait ... If you do not have a PDF reader installed, install <a href= "https://acrobat.adobe.com/us/en/products/pdf-reader.html" >pdf reader </a> </
 Div> </div>

The following is the effect of the Finereport, view screenshots.

The local effect is similar to the following figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.