0

My requirement is to develop an app using IBM Worklight 6.1 which will get the file from web server and show it to the user without downloading it.

For that i did the following steps:-

  1. First Created an Worklight Project
  2. In that project created an java package and trying to access the PDF file from Apache Tomcat.
  3. I am using PDFBOX to read the file from Apache tomcat.
  4. I am going to receive the PDF text from adapter.
  5. From the adapter, i am going to send it to the client side and showing to the user.

I followed the following link to generate JAVA code:

http://www.ehow.com/how_6582916_read-pdf-file-java.html

The code is as follows:-

public static String sendPDF(){
    PDFTextParser pdf = new PDFTextParser("path to pdf in tomcat");
    return pdf.getParsedText();
}

Now the problem is:-

Using this path "path to pdf file in tomcat" the pdfbox is unable to access the file. I am trying to use servletcontext, so i included servlet-api.jar. Still i am unable to access the pdf file. I need a way to access the file. Thanks in advance

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
sasi
  • 512
  • 4
  • 27
  • 1. Can you provide the url what you have tried to access the file from webserver. 2.can you please share if any errors displayed. – dhineshsundar Jun 10 '14 at 12:58
  • 1. The url is http://localhost:8080/pdf/example.pdf 2.The error displaying is " file http://localhost:8080/pdf/example.pdf is not found " – sasi Jun 10 '14 at 13:13
  • I can able to read the file from any drive. But the problem is accessing the file in tomcat – sasi Jun 10 '14 at 13:28
  • I updated my answer in case the problem was to open an URL instead of a local file. – Tilman Hausherr Jun 10 '14 at 13:34
  • The problem is to accessing file from Tomcat Server. So i need to know how exactly we can access it. – sasi Jun 10 '14 at 13:42
  • Maybe clarify - is it a file or an URL? If it is an URL, you can't access it without converting it to an inputStream. – Tilman Hausherr Jun 10 '14 at 17:53
  • Duplicate question http://stackoverflow.com/questions/19839302/worklight-adapter-getting-pdf-file-from-rest-service/19920764#19920764 – Sam Nunnally Jun 14 '14 at 14:24

1 Answers1

1

The correct way to open a PDF url with PDFBox is like this:

PDDocument doc = PDDocument.load(new URL("http://www.host.com/xxx.pdf").openStream());

Even better is to use the non sequential parser:

PDDocument doc = PDDocument.loadNonSeq(new URL("http://www.host.com/xxx.pdf").openStream(), null);

To learn more about text extraction, look at the source code of ExtractImages, which you will find here.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97