943,163 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 2541
  • ASP.NET RSS
Feb 18th, 2010
0

PDF generation question

Expand Post »
I a method that generates a PDF and the method returns a byte value, but after response.end statement, the shows an error that the "File does not begin with '%PDF-' here is the code:
ASP.NET Syntax (Toggle Plain Text)
  1. private static Byte[] GenerateReports(string templateFilePath, DataTable table, string fileName)
  2. {
  3. Byte[] Output = null;
  4. string FilePath;
  5.  
  6. FilePath = Setting.WorkingFolder + "\\GeneratePDF\\" + fileName;
  7. PdfReader reader = null;
  8. FileStream output = new FileStream(fileName, FileMode.Create);
  9. reader = new PdfReader(templateFilePath);
  10.  
  11. PdfStamper stamper = null;
  12.  
  13. stamper = new PdfStamper(reader, output);
  14.  
  15. AcroFields form = stamper.AcroFields;
  16.  
  17. if (table.Rows.Count != 0)
  18. {
  19. foreach (DataRow row in table.Rows)
  20. {
  21.  
  22. form.SetField("mIncome", row["Income"].ToString());
  23. form.SetField("mEnter", row["Enterntainment"].ToString());
  24. form.SetField("mSav", row["Savings"].ToString());
  25. form.SetField("mHealth", row["Health"].ToString());
  26. form.SetField("mTrans", row["Transportation"].ToString());
  27. form.SetField("mSupplies", row["Daily_Supplies"].ToString());
  28. form.SetField("mCharity", row["Charity"].ToString());
  29. form.SetField("mObli", row["Obligations"].ToString());
  30. form.SetField("mHome", row["Home_expenses"].ToString());
  31. form.SetField("mIBal", row["InitialBalance"].ToString());
  32. form.SetField("mcBal", row["CurrentExpenses"].ToString());
  33. form.SetField("mFbal", row["FinalBalance"].ToString());
  34. }
  35. }
  36.  
  37. Output = new byte[output.Length];
  38. output.Write(Output, 0, Convert.ToInt32(output.Length));
  39. stamper.FormFlattening = true;
  40.  
  41. stamper.Close();
  42. reader.Close();
  43.  
  44.  
  45.  
  46. return Output;

ASP.NET Syntax (Toggle Plain Text)
  1. public static Byte[] PrintReport(DataTable data)
  2. {
  3. return GeneratePDF.GenerateReports(Setting.TemplatePath + "ReportTemplate.pdf", data, "Report.pdf");
  4. }

ASP.NET Syntax (Toggle Plain Text)
  1. Response.ContentType = "application/pdf";
  2. Response.AddHeader("Content-Desposition", "attachment; filename = Report.pdf");
  3. Response.BinaryWrite(planner.PrintReport(td));
  4. Response.End();
Last edited by capiono; Feb 19th, 2010 at 12:14 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
capiono is offline Offline
22 posts
since Nov 2009
May 3rd, 2010
0
Re: PDF generation question
Hi,

The problem is the filestream.write reference. Easy mistake to make, the semantics are messed up.

http://msdn.microsoft.com/en-us/libr...eam.write.aspx

When you say:

output.write(byte[], offset, length)

You are actually asking it to take bytes out of the array and put them into the stream.

What you want is output.read(byte[], offset, length).

While they may end up producing a roughly similar byte array, the results of output.write will be not be correct.

In short

ASP.NET Syntax (Toggle Plain Text)
  1. #
  2. Output = new byte[output.Length];
  3. #
  4. output.Read(Output, 0, Convert.ToInt32(output.Length));
  5. #
  6. stamper.FormFlattening = true;
Last edited by Kreadus; May 3rd, 2010 at 1:23 pm. Reason: Example
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kreadus is offline Offline
1 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Dazed and Confused
Next Thread in ASP.NET Forum Timeline: problem using javascript with asp.net button my code is given please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC