I want to generate an image of an Invoice printed for clients and store the image in a Invoice_Archive database for easy retrieval. I would ideally like to take a “snapshot” of the invoice and store it as a Jpg in a media field but I cannot figure out how to capture an image “snapshot” of an entire form and then store it as a jpg.
I would be happy to print the Invoice as a pdf to a temp file, then import and store the pdf in the database and allow viewing of that pdf by the user. I have been able to print the pdf and store it to a temp file, but I cannot figure out how to store the pdf in the database and subsequently view the pdf.
When you create the invoice using a servoy form it is best to save it as a pdf.
When you use jasper forms for it there are more options.
To store the image back you can load is as a byte from a file and save the data.
but I cannot figure out how to store the pdf in the database and subsequently view the pdf.
See the above for storing, the docs, tooltips and sample data should be able to help you further.
Viewing the pdf: you can either use the jpedal if I am not mistaken (search for jpedal and you will find a couple of posts on it, I have never used it).
Or you make a button that opens the pdf with the installed default application on the system.
CarmenScoma:
I would be happy to print the Invoice as a pdf to a temp file, then import and store the pdf in the database and allow viewing of that pdf by the user.
The following code is for Windows server and webclient. Be sure to first create a folder named “pdf” in the Root folder of the server.
To save form as a pdf and place in a media field:
databaseManager.saveData();
if(application.getApplicationType()==5)
{
var installdir = java.lang.System.getProperty("user.dir")
var rnd = Math.round(Math.random()*(99999-11111)+1);
var filename = 'Form'+rnd+'.pdf';
var filepath = installdir+'/server/webapps/ROOT/pdf/'+filename;
controller.print( false,true,plugins.pdf_output.getPDFPrinter(filepath));
//application.showURL('/pdf/'+filename,'_self')
//save pdf to media field
mediafield = plugins.file.readFile(filepath);
plugins.file.deleteFile(filepath);
}
To write pdf from media field to disk and then view the pdf:
if(application.getApplicationType()==5)
{
var installdir = java.lang.System.getProperty("user.dir")
var rnd=Math.round(Math.random()*(99999-11111)+1);
var filename = 'Form'+rnd+'.pdf';
var filepath = installdir+'/server/webapps/ROOT/pdf/'+filename;
var success = plugins.file.writeFile(filepath,mediafield);
if (success)
{
application.showURL('/pdf/'+filename,'_self')
}
}
Thanks for your suggestions. I prefer the second option. Can you tell me what function or code to use to open the pdf as you suggested. I I have stored the pdf in the database with the following code but I don’t know how to access/open the pdf once it is in the database.
var tempFile = plugins.file.createTempFile(‘test’, ‘.pdf’');
plugins.file.writeFile(TempFile,forms.billing_entries_date_print);
var image = plugins.images.getImage(‘c:/test.df’);
forms.Invoice_image.invoice_image = image;
Or you make a button that opens the pdf with the installed default application on the system.
I build the filename like this: day-UUID.pdf, using the current day of the month and a UUID generated by application.getNewUUID then I have a batch processor that cleans up the directory on a daily basis.
The BP just deletes all the pdf files of the previous day. Not so elegant but it’s working fine.
this tempfile idea sounds good, but I guess there are some rules to make this work:
a) which folder is cleaned by the Servoy client? (So actually: in which folder should we write the tempfile)
b) is there any kind of naming convention to achieve this?
Has anyone found a way to get web client to create a pdf and download/display it all in one method?
Using similar to the above I can get the pdf created and stored in a media field and I can separately launch it, but if I try to do both in the same method it doesn’t work.
drookwood:
Has anyone found a way to get web client to create a pdf and download/display it all in one method?
Using similar to the above I can get the pdf created and stored in a media field and I can separately launch it, but if I try to do both in the same method it doesn’t work.
To created the pdf and store it in a media field in a new record in the docs table…
var filename = forms.reports_card.report_title;
var tf = plugins.file.createTempFile(filename, '.pdf');
forms.reports_print.controller.print(true,true,plugins.pdf_output.getPDFPrinter(tf));
var f = plugins.file.readFile(tf);
//create the record here
var n = forms.docs_table_list.foundset.newRecord(false,false);
var record = forms.docs_table_list.foundset.getRecord(n)
record.media = f;
record.filename = normalizedFileName;//(normalizedFileName = just filename plus extension, e.g. report.pdf)
//...other fields are set here also but not relevant to this example
databaseManager.saveData();
This all works fine with the pdf created and stored.
Then separately, to download and view the pdf in webclient…
application.showURL(forms.docs_card.filename)
Again this works fine to open the saved pdf.
BUT if I string both the above code sections together in one method, the pdf is saved OK but does not then also open and I get a browser error:
HTTP Status 404 -
type Status report
message
description The requested resource () is not available.
Apache Tomcat/6.0-snapshot
So despite the pdf being created and stored (and opening fine otherwise) it appears that the webclient cannot find the record to open it if I try to open it as part of the same method that creates the pdf.
You may have to first write the file to the root directory and then open the file with the showURL:
if(application.getApplicationType()==5)
{
var installdir = java.lang.System.getProperty("user.dir")
installdir = utils.stringReplace(installdir,'\\','\/');//original code edited to include this line
var uuid =application.getNewUUID();
var filename = uuid+'.pdf';
var filepath = installdir+'/server/webapps/ROOT/'+filename;
var success = plugins.file.writeFile(filepath,mediafield);
if (success)
{
application.showURL('/'+filename,'_self')
}
}
I did try that before going down the route of writing the file to a media field first, but with the same result - error claiming file could not be found despite being clearly visible in the directory. It seems to me that with web client anyway, the file is not actually visible until the method completes, so trying to create it and launch it all within the same method fails, whereas creating it one method and then separately launching it from a new button click works fine.
drookwood:
…It seems to me that with web client anyway, the file is not actually visible until the method completes, so trying to create it and launch it all within the same method fails, whereas creating it one method and then separately launching it from a new button click works fine.
We do this with one method, so that is not the problem. I did just notice that the following should be inserted as the second line in my above example:
After more trial and error I do now have this working Thanks for the help.
It would be nice now to get it work with a temp file rather than creating a file on the server which then will have to be cleaned out later. However if I try to write the pdf to a temp file I get errors again. Have you tried this?