Création d’un fichier PDF à partir d’un rapport RDLC en arrière-plan

J’exécute un processus de fin de mois et je souhaite qu’il crée automatiquement certains des rapports devant être créés à ce moment-là. J’utilise des rapports rdlc. Existe-t-il un moyen de créer automatiquement un fichier PDF à partir d’un rapport RDLC en arrière-plan?

C’est facile à faire, vous pouvez rendre le rapport au format PDF et enregistrer le tableau d’octets résultant sous forme de fichier PDF sur le disque. Pour faire cela en arrière-plan, c’est plutôt une question de comment votre application est écrite. Vous pouvez simplement lancer un nouveau thread, ou utiliser un BackgroundWorker (s’il s’agit d’une application WinForms), etc. Bien sûr, il peut y avoir des problèmes de multithreading à connaître.

Warning[] warnings; ssortingng[] streamids; ssortingng mimeType; ssortingng encoding; ssortingng filenameExtension; byte[] bytes = reportViewer.LocalReport.Render( "PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); using (FileStream fs = new FileStream("output.pdf", FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); } 

Vous pouvez utiliser le code suivant qui génère un fichier pdf en arrière-plan comme sur un clic de bouton, puis apparaît dans brwoser avec l’option SaveAs et cancel.

 Warning[] warnings; ssortingng[] streamIds; ssortingng mimeType = ssortingng.Empty; ssortingng encoding = ssortingng.Empty;`enter code here` ssortingng extension = ssortingng.Empty; DataSet dsGrpSum, dsActPlan, dsProfitDetails, dsProfitSum, dsSumHeader, dsDetailsHeader, dsBudCom = null; enter code here //This is optional if you have parameter then you can add parameters as much as you want ReportParameter[] param = new ReportParameter[5]; param[0] = new ReportParameter("Report_Parameter_0", "1st Para", true); param[1] = new ReportParameter("Report_Parameter_1", "2nd Para", true); param[2] = new ReportParameter("Report_Parameter_2", "3rd Para", true); param[3] = new ReportParameter("Report_Parameter_3", "4th Para", true); param[4] = new ReportParameter("Report_Parameter_4", "5th Para"); DataSet dsData= "Fill this dataset with your data"; ReportDataSource rdsAct = new ReportDataSource("RptActDataSet_usp_GroupAccntDetails", dsActPlan.Tables[0]); ReportViewer viewer = new ReportViewer(); viewer.LocalReport.Refresh(); viewer.LocalReport.ReportPath = "Reports/AcctPlan.rdlc"; //This is your rdlc name. viewer.LocalReport.SetParameters(param); viewer.LocalReport.DataSources.Add(rdsAct); // Add datasource here byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings); // byte[] bytes = viewer.LocalReport.Render("Excel", 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. // System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Buffer = true; Response.Clear(); Response.ContentType = mimeType; Response.AddHeader("content-disposition", "attachment; filename= filename" + "." + extension); Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file Response.Flush(); // send it to the client to download Response.End(); 

Vous n’avez pas besoin d’avoir un contrôle ReportViewer n’importe où – vous pouvez créer le LocalReport à la volée:

 var lr = new LocalReport { ReportPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? @"C:\", "Reports", "PathOfMyReport.rdlc"), EnableExternalImages = true }; lr.DataSources.Add(new ReportDataSource("NameOfMyDataSet", model)); ssortingng mimeType, encoding, extension; Warning[] warnings; ssortingng[] streams; var renderedBytes = lr.Render ( "PDF", @"PDFFalse", out mimeType, out encoding, out extension, out streams, out warnings ); var saveAs = ssortingng.Format("{0}.pdf", Path.Combine(tempPath, "myfilename")); var idx = 0; while (File.Exists(saveAs)) { idx++; saveAs = ssortingng.Format("{0}.{1}.pdf", Path.Combine(tempPath, "myfilename"), idx); } using (var stream = new FileStream(saveAs, FileMode.Create, FileAccess.Write)) { stream.Write(renderedBytes, 0, renderedBytes.Length); stream.Close(); } lr.Dispose(); 

Vous pouvez également append des parameters: (lr.SetParameter()) , gérer les sous-rapports: (lr.SubreportProcessing+=YourHandler) , ou à peu près tout ce à quoi vous pouvez penser.

Le code ci-dessous fonctionne bien avec moi bien sûr pour les commentaires ci-dessus. Vous pouvez append un visualiseur de rapports et modifier le visible = false et utiliser le code ci-dessous sur le bouton d’envoi:

 protected void Button1_Click(object sender, EventArgs e) { Warning[] warnings; ssortingng[] streamIds; ssortingng mimeType = ssortingng.Empty; ssortingng encoding = ssortingng.Empty; ssortingng extension = ssortingng.Empty; ssortingng HIJRA_TODAY = "01/10/1435"; ReportParameter[] param = new ReportParameter[3]; param[0] = new ReportParameter("CUSTOMER_NUM", CUSTOMER_NUMTBX.Text); param[1] = new ReportParameter("REF_CD", REF_CDTB.Text); param[2] = new ReportParameter("HIJRA_TODAY", HIJRA_TODAY); byte[] bytes = ReportViewer1.LocalReport.Render( "PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings); Response.Buffer = true; Response.Clear(); Response.ContentType = mimeType; Response.AddHeader( "content-disposition", "attachment; filename= filename" + "." + extension); Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file Response.Flush(); // send it to the client to download Response.End(); } 
  private void PDFExport(LocalReport report) { ssortingng[] streamids; ssortingng minetype; ssortingng encod; ssortingng fextension; ssortingng deviceInfo = "" + " EMF" + " 8.5in" + " 11in" + " 0.25in" + " 0.25in" + " 0.25in" + " 0.25in" + ""; Warning[] warnings; byte[] rpbybe = report.Render("PDF", deviceInfo, out minetype, out encod, out fextension, out streamids, out warnings); using(FileStream fs=new FileStream("E:\\newwwfg.pdf",FileMode.Create)) { fs.Write(rpbybe , 0, rpbybe .Length); } 

}

Vous pouvez installer LocalReport

  FicheInscriptionBean fiche = new FicheInscriptionBean(); fiche.ToFicheInscriptionBean(inscription);List list = new List(); list.Add(fiche); ReportDataSource rds = new ReportDataSource(); rds = new ReportDataSource("InscriptionDataSet", list); // attachement du QrCode. ssortingng ssortingngToCode = numinscription + "," + inscription.Nom + "," + inscription.Prenom + "," + inscription.Cin; Bitmap BitmapCaptcha = PostulerFiche.GenerateQrCode(fiche.NumInscription + ":" + fiche.Cin, Brushes.Black, Brushes.White, 200); MemoryStream ms = new MemoryStream(); BitmapCaptcha.Save(ms, ImageFormat.Gif); var base64Data = Convert.ToBase64Ssortingng(ms.ToArray()); ssortingng QR_IMG = base64Data; ReportParameter parameter = new ReportParameter("QR_IMG", QR_IMG, true); LocalReport report = new LocalReport(); report.ReportPath = Page.Server.MapPath("~/rdlc/FicheInscription.rdlc"); report.DataSources.Clear(); report.SetParameters(new ReportParameter[] { parameter }); report.DataSources.Add(rds); report.Refresh(); ssortingng FileName = "FichePreinscription_" + numinscription + ".pdf"; ssortingng extension; ssortingng encoding; ssortingng mimeType; ssortingng[] streams; Warning[] warnings; Byte[] mybytes = report.Render("PDF", null, out extension, out encoding, out mimeType, out streams, out warnings); using (FileStream fs = File.Create(Server.MapPath("~/rdlc/Reports/" + FileName))) { fs.Write(mybytes, 0, mybytes.Length); } Response.ClearHeaders(); Response.ClearContent(); Response.Buffer = true; Response.Clear(); Response.Charset = ""; Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + FileName + "\""); Response.WriteFile(Server.MapPath("~/rdlc/Reports/" + FileName)); Response.Flush(); File.Delete(Server.MapPath("~/rdlc/Reports/" + FileName)); Response.Close(); Response.End();