Question
Virtusa Consulting Services
US
Last activity: 25 Oct 2017 8:54 EDT
How to open a file(pdf/xls) in local machine from pega
Hi,
I have a requirement where i need to open a file located in my Local machine that i need to access from Pega, and i know the designated file path.
Could can use some one expertise here.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Pegasystems Inc.
US
Hi,
Can you please elaborate on your requirement? Do you need to read data from the file for use in some processing within your Pega Application? What types of files are you attempting to open (you specifically mentioned pdf and xls files in the subject of your post)?
Thanks!
-Kurt
Virtusa Consulting Services
US
Hi Kurt,
There is pdf file in my local machine(C:/XXXX), i need to read the content of the file and open with in Pega.
Pegasystems Inc.
US
Hi,
This isn't something I've personally attempted before, so someone else might be able to offer more guidance. I did, however, find some posts that have some information that sounds like it may be of use to you:
https://collaborate.pega.com/question/need-read-data-doc-and-pdf
https://community.pega.com/sites/default/files/help_v731/procomhelpmain.htm
Hope this helps!
-Kurt
Pegasystems Inc.
GB
So : firstly you should probably avoid directly reading from full paths in your PRPC activities (or indeed any web application); if you really do need to read from a particular file location ('outside' of the PRPC Web App) - PRPC has the concept of a FileListener which can monitor designated directories for files.
Another option: if you can copy your PDF to the 'ServiceExport' directory in your PRPC system (this is where uploads/downloads of things like ZIP files / RAP files take place) - this is located in your 'PegaTemp' directory like this:
[Path to 'PegaTemp']/StaticContent/global/ServiceExport
You can read files from this directory using the Pega Engine API; for instance the following Java Activity Step will read a PDF from the directory and extract the text from it (using 'PDFBox').
try {
PRInputStream pris=new PRInputStream( new PRFile( PDFPath) );
org.apache.pdfbox.pdmodel.PDDocument doc=org.apache.pdfbox.pdmodel.PDDocument.load( pris );
org.apache.pdfbox.text.PDFTextStripper pdTextStripper=new org.apache.pdfbox.text.PDFTextStripper();
text=pdTextStripper.getText( doc );
}
catch(Exception e) {
throw new PRRuntimeException(e);
}
The Local Variable 'PDFPath' is first set up (using a 'Property-Set' method) like this:
So : firstly you should probably avoid directly reading from full paths in your PRPC activities (or indeed any web application); if you really do need to read from a particular file location ('outside' of the PRPC Web App) - PRPC has the concept of a FileListener which can monitor designated directories for files.
Another option: if you can copy your PDF to the 'ServiceExport' directory in your PRPC system (this is where uploads/downloads of things like ZIP files / RAP files take place) - this is located in your 'PegaTemp' directory like this:
[Path to 'PegaTemp']/StaticContent/global/ServiceExport
You can read files from this directory using the Pega Engine API; for instance the following Java Activity Step will read a PDF from the directory and extract the text from it (using 'PDFBox').
try {
PRInputStream pris=new PRInputStream( new PRFile( PDFPath) );
org.apache.pdfbox.pdmodel.PDDocument doc=org.apache.pdfbox.pdmodel.PDDocument.load( pris );
org.apache.pdfbox.text.PDFTextStripper pdTextStripper=new org.apache.pdfbox.text.PDFTextStripper();
text=pdTextStripper.getText( doc );
}
catch(Exception e) {
throw new PRRuntimeException(e);
}
The Local Variable 'PDFPath' is first set up (using a 'Property-Set' method) like this:
Property-Set local.PDFPath <- .pxServiceExportPath + Param.pdf_filename [ StepPage:pxProcess ]
Then you can view the contents of the Local Variable 'text' using a 'Show-Property'.
Show-Property Local.text
Here's some screenshots of such an example Activity:
And here's it running (using this example PDF) :
(The Browser shows this text all squashed together; but in fact (View Source shows this) the text *does* have preserved linebreaks etc):
This was done on PRPC73: if you have an earlier version of PRPC - you might find you need to adjust the package names.
Check 'pr_engineclasses' table if unsure; eg:
select
pzpackage,pzclass from pr_engineclasses
where
lower(pzjar) like '%pdfbox%'
and lower(pzclass) like '%stripper%';
Virtusa Consulting Services
US
Hi JOHNPW_GCS
I believe the above process is for the file stored in Pega directory, where in and as i'm trying to fetch the file from local machine like (C:/ Users/Downloads), and without using File listener can we achieve this?
Pegasystems Inc.
GB
Hi Sumpathd,
It is not possible for PRPC (or any WebApp for that matter) to see the contents of your Local File System : you wouldn't want this anyway - for security reasons.
The only way PRPC (or any other webapp) can 'see' files on your local machine, would be for you to first upload them to the Server ; or use some sort of (trusted) Browser Plugin that is given special permission to look at files on your local machine.
I would suggest you need to build a mechanism to upload the local file to PRPC; and then process it.
You could just use the OOTB PRPC 'Attachments' facility for this - where you attach a file to a Work Item ('Case') ; then process the attachments themselves.
Thanks,
John