Storing pdfs in sql server generates format error

Storing pdfs in sql server generates format error

Forum member · Jun 5, 2008
Hi, Initially I was mailing PDF's as attachments in real time when a user clicked on a web button to mail the client. This was taking a long time so I have modified my system (probably for the better) to store the mails in the database and have a simple service send these mails to the relevant recipients. My issue is that the email that is received by the recipient indicates that the stored PDF I have taken from the database and attached to the mail is incorrect or corrupt.. I am using .NET 2 visual studio 2005. I am not sure if this is something I am doing wrong in the storage routine or something wrong in the collating routine. I am using an image type on the database, and my gut feeling says its something yo do with encoding. The image does have data and the file contains data, so I am not sure what I am doing wrong. Maybe I have to use a BinaryWriter? Code below and would really appreciate anyones input on this Many thanks Richard CODE: Storage: ///Create a memory stream MemoryStream mstr = new MemoryStream(); ///Create a generic document IPDFDocument document = DocumentFactory.CreateInvoiceDocument(invoice); ///Render calls the Draw method in the PdfDocument from Cete document.Render(mstr); //Gets the byte array within the memory stream and assign it to the image Byte[] b = new Byte[mstr.Length]; mstr.Read(b, 0, b.Length); mstr.Close(); ///Set the data so the entity can be stored email.EmailAttachment = b; (This is a byte[] type (image)) COLLATING //method to create the pdf document from the byte array protected string FileFromBytes(byte[] data, string filename) { string path = Path.GetTempPath()+filename; FileStream file = null; try { if (File.Exists(path)) { File.Delete(path); } file = new FileStream(path, FileMode.CreateNew); file.Write(data, 0, data.Length); } catch (IOException ioexc) { LogError("Email attachment failed to be generated", ioexc); throw ioexc; } finally { if (file != null) { file.Close(); } } return path; }

Re: Storing pdfs in sql server generates format error

Forum member · Jun 5, 2008
Hi Guys, Doing a specific Draw() instead of Draw(Stream s) method instead of drawing to a stream then writing to a byte array works. Thanks