Tech Off Thread

16 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Creating pdf from byte array in c#

Back to Forum: Tech Off
  • User profile image
    venkatzeus

    Hi,

    I am using C#. I am trying to create a pdf file from byte array.

     

    I tried the below code in the page load. The variable blob contains the byte array data.

     

     
    Response.Clear(); 
    Response.AddHeader("Content-Length", blob.Length.ToString()); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("Accept-Ranges", "bytes"); 
    Response.Buffer = true; 
    Response.AddHeader("Expires", "0"); 
    Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 
    Response.AddHeader("Pragma", "public"); 
    Response.AddHeader("content-Transfer-Encoding", "binary"); 
    Response.AddHeader("Content-Disposition", "attachment; filename=sample.pdf"); 
    Response.BinaryWrite(blob); 
    Response.Flush(); 
    Response.End();
    

     

    The pdf is getting saved. But when I try to open the pdf, I am getting the error as " Acrobat could not open 'sample.pdf' because it is either not a supported file type or because the file has been damaged"

     

    How to create a pdf file from a byte array?

     

    Thank you

  • User profile image
    W3bbo

    You're not creating a PDF, you're just serving the raw bytes of a PDF to the client.

     

    Where are you getting your bytes from?

  • User profile image
    venkatzeus

    W3bbo said:

    You're not creating a PDF, you're just serving the raw bytes of a PDF to the client.

     

    Where are you getting your bytes from?

    Hi,

    Thank you very much for the reply.

    I am converting a serilaized data,which i recieve from a web service to a byte array.

     

    I am missing anything. Thank you

  • User profile image
    Sven Groot

    venkatzeus said:
    W3bbo said:
    *snip*

    Hi,

    Thank you very much for the reply.

    I am converting a serilaized data,which i recieve from a web service to a byte array.

     

    I am missing anything. Thank you

    There is no way that serializing an object could yield a valid PDF. How were you expecting this to become a PDF?

  • User profile image
    venkatzeus

    Sven Groot said:
    venkatzeus said:
    *snip*

    There is no way that serializing an object could yield a valid PDF. How were you expecting this to become a PDF?

    I am converting to a byte array, and then using byte array to create a pdf.

     

     

  • User profile image
    Sven Groot

    venkatzeus said:
    Sven Groot said:
    *snip*

    I am converting to a byte array, and then using byte array to create a pdf.

     

     

    No, that's not what you're doing at all. You're creating a byte array, containing a serialized object graph (which is not valid PDF data), and then telling the browser to interpret that data as if it were a PDF.

     

    You are creating a PDF file, but the contents you supply for that file (the serialized object graph) is not a valid PDF document, so of course Adobe Reader can't open it. There is no magic process that can convert a serialized object graph to a PDF.

     

    What are you trying to do here exactly? What kind of data is that web service returning, and how is that data meant to be presented as a PDF?

  • User profile image
    venkatzeus

    Sven Groot said:
    venkatzeus said:
    *snip*

    No, that's not what you're doing at all. You're creating a byte array, containing a serialized object graph (which is not valid PDF data), and then telling the browser to interpret that data as if it were a PDF.

     

    You are creating a PDF file, but the contents you supply for that file (the serialized object graph) is not a valid PDF document, so of course Adobe Reader can't open it. There is no magic process that can convert a serialized object graph to a PDF.

     

    What are you trying to do here exactly? What kind of data is that web service returning, and how is that data meant to be presented as a PDF?

    Hi,

    Thank you very much for the reply. Thanks for correcting me.

     

    The web service returns a pdf file as a serialized data, which has the return type as blob.

  • User profile image
    Sven Groot

    venkatzeus said:
    Sven Groot said:
    *snip*

    Hi,

    Thank you very much for the reply. Thanks for correcting me.

     

    The web service returns a pdf file as a serialized data, which has the return type as blob.

    Using the BinaryFormatter.Serialize method doesn't just write out the raw data of the object. It adds all kinds of stuff to ensure that the data can be safely deserialized again, like .Net type names. So the result of this method will definitely not be a valid PDF file.

     

    If the web service's return value indeed contains a PDF document in some format, you need to properly extract that data somehow. Without knowing the web services actual return type I can't help you with that. What is the type of the object returned by the web method?

  • User profile image
    venkatzeus

    Sven Groot said:
    venkatzeus said:
    *snip*

    Using the BinaryFormatter.Serialize method doesn't just write out the raw data of the object. It adds all kinds of stuff to ensure that the data can be safely deserialized again, like .Net type names. So the result of this method will definitely not be a valid PDF file.

     

    If the web service's return value indeed contains a PDF document in some format, you need to properly extract that data somehow. Without knowing the web services actual return type I can't help you with that. What is the type of the object returned by the web method?

    The Web Method returns a blob

  • User profile image
    Sven Groot

    venkatzeus said:
    Sven Groot said:
    *snip*

    The Web Method returns a blob

    What's a blob? I know what it is in database terms, but in C# terms it means nothing.

     

    What's the .Net type, the class name, of the web method's return value?

  • User profile image
    venkatzeus

    Sven Groot said:
    venkatzeus said:
    *snip*

    What's a blob? I know what it is in database terms, but in C# terms it means nothing.

     

    What's the .Net type, the class name, of the web method's return value?

    In the webreference, when I add the reference, I get the following:

     

    WebMethodName ( id As int ) As BLOB
    

  • User profile image
    Sven Groot

    venkatzeus said:
    Sven Groot said:
    *snip*

    In the webreference, when I add the reference, I get the following:

     

    WebMethodName ( id As int ) As BLOB
    

    There is no built-in .Net type by that name, so it must be a type defined by the service contract. Without knowing more about the contract, I can therefore not help you any further.

     

    Can you give me the URL of the web service you are using? Or the WSDL contract? Or at the very least the generated type definition for BLOB?

     

    I suspect the blob is a simple container for arbitrary binary data. But without knowing the definition of the type, I have no idea how you could access the data.

  • User profile image
    W3bbo

    Sven Groot said:
    venkatzeus said:
    *snip*

    There is no built-in .Net type by that name, so it must be a type defined by the service contract. Without knowing more about the contract, I can therefore not help you any further.

     

    Can you give me the URL of the web service you are using? Or the WSDL contract? Or at the very least the generated type definition for BLOB?

     

    I suspect the blob is a simple container for arbitrary binary data. But without knowing the definition of the type, I have no idea how you could access the data.

    Sven, I can't help but read this thread with amusement. I think it's worth giving up, there's no hope for venkatzeus here; I get the impression he has absolutely zero hold on any of the concepts involved.

  • User profile image
    Clint

    My quick advice would be to attempt to just save the blob to disk and see if that yields a valid PDF that can be opened.  That will tell you if your response code is weird or your data return is.

     

    I used websupergoo abcPDF at a different company, was pretty effective.

  • User profile image
    venkatzeus

    W3bbo said:
    Sven Groot said:
    *snip*

    Sven, I can't help but read this thread with amusement. I think it's worth giving up, there's no hope for venkatzeus here; I get the impression he has absolutely zero hold on any of the concepts involved.

    Hi,

     

    I got the solution. One of the methods had a Url, which pointed to the pdf. I then used that with Process.start and invoked the pdf.

     

    Thanks Sven Groot and all for the help.

     

     

     

  • User profile image
    MikeWebb

    , venkatzeus wrote

    *snip*

    Hi,

     

    I got the solution. One of the methods had a Url, which pointed to the pdf. I then used that with Process.start and invoked the pdf.

     

    Thanks Sven Groot and all for the help.

     

     

     

     

    How did you fix it? Can you post your code? I got the same issue : Covert byte array to string using base64 encoding, embed it in a xmlDocument, at client side, decode, write the stream to pdf, then got that error.

     

    Thanks.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.