• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3243
  • Last Modified:

WCF Rest Service Returning Corrupted PDF

I have a WCF REST service that is set up to return a pdf. I've got it to where the browser recognized that there is, in fact, a pdf being returned and it will save. The resulting pdf, however, seems to be corrupted as Acrobat won't open it. The Error is "Adobe Reader could not open file.pdf because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

Just for reference, the FormFactory object's "BuildForm()" method creates a pdf on the fly and returns it as a memory stream. I have verified that this works just fine with a normal aspx page and response.binarywrite etc etc.

I have also tried it with the method returning a Stream as well as MemoryStream. It's as Byte[] now because that was just the last thing I tried. They've all had the same exact result.

[ServiceContract]
    public interface IMakeForm
    {
        [OperationContract(Name = "BuildIt")]
        [WebGet(UriTemplate="/BuildIt", BodyStyle=WebMessageBodyStyle.Bare)]
        Byte[] BuildIt();
    }

Open in new window


public class MakeForm : IMakeForm
    {

        public Byte[] BuildIt()
        {

            FormFactory ff = new FormFactory();

           
            string pdfFile = "frm5.pdf";  //just a placeholder for now until I get this working
            MemoryStream stream = ff.BuildForm(pdfFile);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            WebOperationContext.Current.OutgoingResponse.ContentLength = stream.Length;
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "attachment; filename=ResultForm_" + DateTime.Now.ToString("HH_mm_ss") + ".pdf");

            return stream.ToArray();

        }
    }

Open in new window


  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false"
      multipleSiteBindingsEnabled="true" />

    <services>
      <service name="FormMaker.MakeForm">
        <endpoint address="" behaviorConfiguration="MakeFormServiceBehavior"
                  binding="webHttpBinding" contract="FormMaker.IMakeForm" />

      </service>

    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="MakeFormServiceBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServBeh">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="httpBinding" transferMode="Streamed" >
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

Open in new window

0
fredstov
Asked:
fredstov
  • 2
1 Solution
 
fredstovAuthor Commented:
I figured it out finally. I had to modify my FormFactory object to return a Byte Array rather than a MemoryStream. Then I just modified my service code to this:

public class MakeForm : IMakeForm
    {

        public Stream BuildIt()
        {
            
            FormFactory ff = new FormFactory();

            
            string pdfFile = "pdfpath.pdf";
         
            
            byte[] pdf = ff.BuildForm(pdfFile);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            WebOperationContext.Current.OutgoingResponse.ContentLength = pdf.Length;
 
            return new MemoryStream(pdf);

        }
    }

Open in new window

0
 
fredstovAuthor Commented:
Figured it out before the someone else did.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now