Web Service return pdf file

Using Servoy to administrate the content of your website? Discuss all webrelated Servoy topics on this forum!

Web Service return pdf file

Postby mmcmanus » Fri Mar 23, 2012 6:21 pm

I've been working on a web service to return assort XML data, but know I wish to return a pdf file with a new method. How do I return the file as the result and not a link to the file?

Thanks all. Just getting used to 6 and all the new toys. Crash building a web service is one of them
mmcmanus
 
Posts: 41
Joined: Thu Feb 23, 2006 7:28 pm
Location: Cincinnati, OH

Re: Web Service return pdf file

Postby Adelo Herrero » Sat Mar 24, 2012 9:48 am

Can you return a byte array with the file content and save it in local?

Best regards.
Adelo Herrero
 
Posts: 103
Joined: Sat Jul 25, 2009 9:24 pm
Location: Requena (Valencia) España

Re: Web Service return pdf file

Postby mmcmanus » Mon Mar 26, 2012 8:19 pm

So how would you return a byte array then as part of the xml stream? Not sure I understand. I have tried writing to byte array, but nothing seems to return. When I was working in Developer I got a save file dialog. On a server nada that I could see.

Something along the lines of:

respondXML[new Object('file')] = plugins.file.writeFile(plugins.file.readFile(fileNameis), bytes,'application/pdf')
mmcmanus
 
Posts: 41
Joined: Thu Feb 23, 2006 7:28 pm
Location: Cincinnati, OH

Re: Web Service return pdf file

Postby ptalbot » Mon Mar 26, 2012 10:53 pm

Or use Velocity's new web capabilities to do this: plugins.Velocity.createByteResponse(bytes, mimeType) ;)
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Web Service return pdf file

Postby ptalbot » Mon Mar 26, 2012 10:54 pm

Actually to embed in XML, you will have to convert to Base64. XML is not a binary format!
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Web Service return pdf file

Postby ptalbot » Tue Mar 27, 2012 12:47 am

It's also possible to save the file on the server somewhere in /application_server/server/webapps/ROOT/ in a subfolder subfolder and return a URL to that file (which will be something like: http://yourserverURL/subfolder/yourPDF.pdf)
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Web Service return pdf file

Postby Adelo Herrero » Tue Mar 27, 2012 1:28 am

If the pdf is in the server, read it in a byte array.

Code: Select all
var bytes = plugins.file.readFile('big.jpg');


The return bytes in DATA section of XML
Code: Select all
var myXML = "....";
myXML+="<fileName>myFile.pdf</fileName>"
myXML+="<fileContent><![CDATA[" + bytes + "]]></fileContent>"
myXML+="..."


And in local side save "bytes" as a file:

Code: Select all
var f = plugins.file.convertToJSFile('myFile.pdf');
plugins.file.writeFile(f, bytes)
Adelo Herrero
Adelo Herrero
 
Posts: 103
Joined: Sat Jul 25, 2009 9:24 pm
Location: Requena (Valencia) España

Re: Web Service return pdf file

Postby ptalbot » Tue Mar 27, 2012 2:23 am

Code: Select all
myXML+="<fileContent><![CDATA[" + bytes + "]]></fileContent>"

will never work!

Because you take binary data and you concatenate to a String, so any non character bytes will be skipped and you will get garbage result...

You need to encode the byte array using Base64 encoding.
You can use Packages.org.apache.commons.codec.binary.Base64.encodeBase64(bytes) that will return a properly encoded String that you can then put into you CDATA section.

And on the receiving side, you will need to decode that Base64 String or course.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Web Service return pdf file

Postby Adelo Herrero » Tue Mar 27, 2012 6:11 am

Really; I need convert to base64. Just missing the most important :O
Adelo Herrero
Adelo Herrero
 
Posts: 103
Joined: Sat Jul 25, 2009 9:24 pm
Location: Requena (Valencia) España

Re: Web Service return pdf file

Postby mmcmanus » Tue Mar 27, 2012 2:48 pm

Excellent help. Thank you. I think I may be on the right track now. I have it encoded and am testing the whole process today. I'll try to post the result when complete.
mmcmanus
 
Posts: 41
Joined: Thu Feb 23, 2006 7:28 pm
Location: Cincinnati, OH

Re: Web Service return pdf file

Postby mmcmanus » Wed Apr 04, 2012 3:40 pm

This worked pretty well. Had some problems with getting the wholebyte array into cdata. And ended up at the clients request moving to just returning a link to the file. Which does cause two gets, but seems to be working better for them at the moment.
mmcmanus
 
Posts: 41
Joined: Thu Feb 23, 2006 7:28 pm
Location: Cincinnati, OH

Re: Web Service return pdf file

Postby ptalbot » Wed Apr 04, 2012 4:56 pm

That's 2 request but the XML is much lighter so it shouldn't be a problem anyway (Bas64 encoding will make your data a lot bigger).
I think that's the best way to go.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC


Return to Web Development

Who is online

Users browsing this forum: No registered users and 9 guests