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.

    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

    <serviceHostingEnvironment aspNetCompatibilityEnabled="false"
      multipleSiteBindingsEnabled="true" />

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



        <behavior name="MakeFormServiceBehavior">
          <webHttp />
        <behavior name="ServBeh">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        <binding name="httpBinding" transferMode="Streamed" >
          <security mode="None" />

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fredstovAuthor Commented:
Figured it out before the someone else did.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Services

From novice to tech pro — start learning today.