a http request can only return one response. to return 5 items, a seperate request must be made for each item. this could be done in javascript, where clicking on a link/button, would make 5 requests with a target "_blank" (open 5 windows). you oter option
if it must be done server side is place the 5 pdfs in a zip file that s returned.
a http request can only return one response. to return 5 items, a seperate request must be made for each item. this could be done in javascript, where clicking on a link/button, would make 5 requests with a target "_blank" (open 5 windows). you oter option
if it must be done server side is place the 5 pdfs in a zip file that s returned.
Thank you bruce! I will try the javascript option you suggested. =)
I'm having the same issue than you and working on the new window for each file solution, if you manage to do it, care to share your code? (I'll do the same if I manage it before you do)
What I did was create a new aspx form. For each file that I want to download, i'm opening a new windows witht eh scriptmanager. The window.open URL has a param which I uses to find which document I want to download in a list of documents that is stored in a
session variable.
basically, for every documents in a list of documents (i generate my pdfs using ActiveReports dll), I do this
Then I put the list in a session variable and I get this session variable in the load event of my other aspx form (that I called DownloadDocument.aspx).
if (Request.Params["index"] != null){
List<ActiveReport> listRapportAAfficher = new List<ActiveReport>();
listRapportAAfficher = (List<ActiveReport>)Session["ListeRapports"];
//Partir le download
int index = Convert.ToInt32(Request.Params["index"].ToString());
ActiveReport rpt = listRapportAAfficher[index];
System.IO.MemoryStream m_stream = new System.IO.MemoryStream();
rpt.Run();
PdfExport pdfExport1 = new DataDynamics.ActiveReports.Export.Pdf.PdfExport();
pdfExport1.Export(rpt.Document, m_stream);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + rpt.Name + ".pdf");
Response.BinaryWrite(m_stream.ToArray());
Response.End();
}
This is just a recommendation. You have a lot of undisposed resources between your memory streams and the iText objects you call. Calling multiple methods with memory streams and undisposed resources can get messy. I'd recommend wrapping 'using' statements
but calling .Dispose() would do the same.
private byte[] ExportToPDF()
{
using (MemoryStream pdfStream = new MemoryStream())
using (Document pdfDoc = new Document())
using (PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream))
{
// your code
return pdfStream.ToArray();
}
}
If you do .Dispose(), make sure you return your stream before you close it or it'll error on you.
danmbuen
Member
688 Points
188 Posts
Exporting multiple PDF
Jan 16, 2013 02:26 PM|LINK
Hi guys!
I have a list in which each item would correspond to a PDF output. So if there are 5 items, the system should also produce 5 PDFs.
This is my code for the loop that reads the list:
foreach(condition...)
{
ExportToPDF(dt, s_Name, s_Address1, s_Address2, s_Address3, s_Customercode, s_AccountName, s_VatNumber);
}
This is the code for exporting datatable to PDF:
private void ExportToPDF(DataTable dt, field1, field 2, .... field x) { Document pdfDoc = new Document(); MemoryStream pdfStream = new MemoryStream(); PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream); pdfDoc.Open();//Open Document to write pdfDoc.NewPage(); PdfPTable PdfTable = new PdfPTable(dt.Columns.Count); PdfPCell PdfPCell = null; //Add Header of the pdf table for (int column = 0; column < dt.Columns.Count; column++) { PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].Caption, font8))); PdfTable.AddCell(PdfPCell); } //How add the data from datatable to pdf table for (int rows = 0; rows < dt.Rows.Count; rows++) { for (int column = 0; column < dt.Columns.Count; column++) { PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8))); PdfTable.AddCell(PdfPCell); } } pdfDoc.Add(PdfTable); // add pdf table to the document pdfDoc.Add(new Phrase(String.Format("Page {0} - ", pdfDoc.PageNumber), endingMessageFont)); pdfDoc.Add(new Phrase(DateTime.Now.ToLongDateString(), endingMessageFont)); pdfDoc.Close(); pdfWriter.Close(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=RunInvoice-{0}.pdf", DateTime.Now.ToShortDateString())); Response.BinaryWrite(pdfStream.ToArray()); Response.End(); }Any help would be appreciated. Thanks!
ignatandrei
All-Star
137688 Points
22150 Posts
Moderator
MVP
Re: Exporting multiple PDF
Jan 16, 2013 02:53 PM|LINK
Either open 5 new url - each one for each pdf -
either put into a zip file and download the zip file
bruce (sqlwo...
All-Star
37614 Points
5573 Posts
Re: Exporting multiple PDF
Jan 16, 2013 02:58 PM|LINK
a http request can only return one response. to return 5 items, a seperate request must be made for each item. this could be done in javascript, where clicking on a link/button, would make 5 requests with a target "_blank" (open 5 windows). you oter option if it must be done server side is place the 5 pdfs in a zip file that s returned.
danmbuen
Member
688 Points
188 Posts
Re: Exporting multiple PDF
Jan 17, 2013 07:30 AM|LINK
Thanks for the suggestion ignatandrei! I'll try the option zip file!
danmbuen
Member
688 Points
188 Posts
Re: Exporting multiple PDF
Jan 17, 2013 07:31 AM|LINK
Thank you bruce! I will try the javascript option you suggested. =)
serder
Member
4 Points
2 Posts
Re: Exporting multiple PDF
Jan 18, 2013 02:39 PM|LINK
I'm having the same issue than you and working on the new window for each file solution, if you manage to do it, care to share your code? (I'll do the same if I manage it before you do)
serder
Member
4 Points
2 Posts
Re: Exporting multiple PDF
Jan 18, 2013 06:15 PM|LINK
Managed to do it server side without a zip.
What I did was create a new aspx form. For each file that I want to download, i'm opening a new windows witht eh scriptmanager. The window.open URL has a param which I uses to find which document I want to download in a list of documents that is stored in a session variable.
basically, for every documents in a list of documents (i generate my pdfs using ActiveReports dll), I do this
string script = ""; script += "window.open(\"~/../DownloadDocument.aspx?index=" + i.ToString() + "\");"; ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType() , i.ToString() + "DlPopup", script, true );Then I put the list in a session variable and I get this session variable in the load event of my other aspx form (that I called DownloadDocument.aspx).
if (Request.Params["index"] != null){ List<ActiveReport> listRapportAAfficher = new List<ActiveReport>(); listRapportAAfficher = (List<ActiveReport>)Session["ListeRapports"]; //Partir le download int index = Convert.ToInt32(Request.Params["index"].ToString()); ActiveReport rpt = listRapportAAfficher[index]; System.IO.MemoryStream m_stream = new System.IO.MemoryStream(); rpt.Run(); PdfExport pdfExport1 = new DataDynamics.ActiveReports.Export.Pdf.PdfExport(); pdfExport1.Export(rpt.Document, m_stream); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=" + rpt.Name + ".pdf"); Response.BinaryWrite(m_stream.ToArray()); Response.End(); }medelbrock
Member
729 Points
153 Posts
Re: Exporting multiple PDF
Jan 18, 2013 09:54 PM|LINK
This is just a recommendation. You have a lot of undisposed resources between your memory streams and the iText objects you call. Calling multiple methods with memory streams and undisposed resources can get messy. I'd recommend wrapping 'using' statements but calling .Dispose() would do the same.
private byte[] ExportToPDF() { using (MemoryStream pdfStream = new MemoryStream()) using (Document pdfDoc = new Document()) using (PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream)) { // your code return pdfStream.ToArray(); } }If you do .Dispose(), make sure you return your stream before you close it or it'll error on you.