|
|
Hi,
i ' m using last release of webflow. I have a problem to show pdf file. It show cripted data with quot;%PDF-1.4%quot; header.
Note that i have been speecified data type as quot;application/pdfquot;.
What is the problem in my code :public Event showPdf( RequestContext context ) throws Exception {
//get response
fromServletResponse response = (fromServletResponse) context.getExternalContext().getNativeResponse();
response.reset();
response.setCharacterEncoding( quot;UTF-8quot; );
response.setContentType( quot;application/pdfquot; );
//response.setContentType(quot;application/octet-streamquot;); response.setHeader( quot;Content-dispositionquot;, quot;inline; filename=test.pdfquot; );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = getPdfService();
OutputStream out = response.getOutputStream();
response.setContentLength(baos.size());
out.write( baos.toString().getBytes());
out.flush();
out.close();
// mark response complete
context.getExternalContext().recordResponseComplet e();
return success();
}
Thanks
Hi!
Doing the
baos.toString()
is wrong because as stated in the java-doc:
Code:
Converts the buffer's contents into a string decoding bytes using the
platform's default character set. The length of the new lt;ttgt;Stringlt;/ttgt;
is a function of the character set, and hence may not be equal to the
size of the buffer.
This method always replaces malformed-input and unmappable-character
sequences with the default replacement string for the platform's
default character set. The {@linkplain java.nio.charset.CharsetDecoder}
class should be used when more control over the decoding process is
required.
So what you are doing is decoding the pdf-byte stream - theirfor the pdf gets converted to String which is wrong.
You should stream from an inputstream to the response Outputstream with something like this:
Code:
protected long stream(InputStream in, OutputStream out, int bufferSize) throws IOException { int read; long bytesWritten = 0; byte buff[] = new byte[bufferSize]; while ((read = in.read(buff, 0, buff.length)) gt; 0) { out.write(buff, 0, read); bytesWritten += read; } return bytesWritten;
}
using ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())
and the Outputstream of the response as parameters for the method above
(probably not the most effecience way - doing to much with ByteArray-Streams always uses much memory) but should work like this
cu
Wolfman
Hi,
I was tried use toByteArray().
but i still have the problem.Are you sure that your
getPdfService();
delivers the right pdf format?
(save it to disk and open pdf file works?)
yes it work fine when saving on disk.
and when displaying into browser, data are like this :
%PDF-1.4 %#65533;#65533;#65533;#65533; 3 0 obj lt;gt;stream x#65533;#65533;ZOs#65533;6#460;n#65533;!#65533;amp;N#65533;'#65533;#65533;#65533;u#1198; #65533;#65533;^:#65533;$;je#598;-O}#65533;4;#65533;#1652;J/{#65533;#65533;~#65533;#65533;#553;#65533;#65533;=%A ,w#1185;)#65533; #65533;#65533;#65533;#65533;#65533;#65533;w]Y#65533;#65533;O#65533;Wh#65533;#65533;#65533;#65533;#65533;u#65533;#65533;;l.2#1758;#65533;#65533;S#65533;@n#65533;#65533;#65533;~w#65533;=#542;#65533;#65533;#65533;]#65533;p#65533;]#627;^.y#65533;{?#65533;#65533;[#65533;#65533;#65533;m#65533;{#65533;:#65533;#65533;f#65533;#65533;#65533;#65533;#8#65533;#65533;#65533;#65533;=f_]#65533;#40892;#65533;#65533;]2QpT#65533;lt;#65533;@#686;#1560;#65533;#1538;#769;z#1678;#65533;#65533;'#65533;}#65533;N#65533;#65533;)#65533;Y#65533;
have you tried do do this in a plain Servlet - so you can exclude that anything of webflow mixes things?
Originally Posted by Elmouryes it work fine when saving on disk.
and when displaying into browser, data are like this :
%PDF-1.4 %#65533;#65533;#65533;#65533; 3 0 obj lt;gt;stream x#65533;#65533;
Look like browser not detect content type and just show content of file as text, not as PDF.
Something forces content type on server side. Try play with headers.
Originally Posted by ElmourHi,
out.write( baos.toString().getBytes());And in any case replase with:Code:
out.write( baos.toByteArray());
baos.toByteArray() is more preffered and can't damage your stream
Originally Posted by ElmourHi,
It show cripted data with quot;%PDF-1.4%quot; header.
And about quot;%PDF-1.4%quot;. Open PDF file in any HEX editor and look on start - it's usualy %PDF-1.3, %PDF-1.4, %PDF-1.5...
It's signature of PDF file :-) ;-)
as recommended before - check if the headers are the right one's - you can check headers with the quot;Live from headersquot; Plugin for Firefox |
|