0

I'm generating pdf file in my project using pdfbox api,initially i just stored the dynamically generated pdf file in my server and now i'm trying to stream pdf file in browser with following code

 Ok.stream(Enumerator.outputStream { os => 
          doc.writeToStream(os)//doc holds content of pdf file
          os.close()
        }).withHeaders(
          CONTENT_TYPE -> "application/pdf",
          CONTENT_DISPOSITION -> "attachment; filename=report.pdf"
        )

i have some response begins as

%PDF-1.4
%����
1 0 obj
<<
/Type /Catalog
/Version /1.4
/Pages 2 0 R
>>
endobj
2 0 obj

hope it looks like pdf file have generated ,but doesnt load in browser ..i dont know what i'm missing,i just want to load pdf file new tab

immutable
  • 2,174
  • 3
  • 20
  • 27

4 Answers4

0

Maybe the Enumerator is not closed properly.

Try this :

Ok.stream(Enumerator.outputStream { os => 
      doc.writeToStream(os)//doc holds content of pdf file
      os.close()
    } >>> Enumerator.eof).withHeaders(
      CONTENT_TYPE -> "application/pdf",
      CONTENT_DISPOSITION -> "attachment; filename=report.pdf"
    )
ndeverge
  • 21,378
  • 4
  • 56
  • 85
0

something like:

val enumerator = Enumerator.outputStream { os =>
  doc.writeToStream(os)
  os.close
}

Ok.stream(enumerator >>> Enumerator.eof).as("application/pdf")

works great in my case too so i suppose there is something wrong with writeToStream. you could do a test by streaming a static image there is nothing special to configure in play framework

0

Try setting your CONTENT_DISPOSITION to "inline":

Ok.stream(Enumerator.outputStream { os => 
      doc.writeToStream(os)//doc holds content of pdf file
      os.close()
    }).withHeaders(
      CONTENT_TYPE -> "application/pdf",
      CONTENT_DISPOSITION -> "inline; filename=report.pdf"
    )

And see if it makes a difference.

It should, even though the specific behavior is browser dependent.

You can find more details regarding the Content-Disposition header here.

Community
  • 1
  • 1
Jack Leow
  • 21,945
  • 4
  • 50
  • 55
  • i just solved now...few problems in js about handling response...now i can open generated pdf in new window...i already tried CONTENT_DISPOSITION -> "inline; filename=report.pdf" ,anyway m accepting ur answer... – immutable Sep 27 '13 at 04:13
0

The content length attribute in the header is missing .

https://www.playframework.com/documentation/2.5.x/ScalaStream

Guillaume agis
  • 3,756
  • 1
  • 20
  • 24