2

I'm working in a servlet file for a web project and this is my code :

I have the v.2.0.0 of pdfbox library and my code works in a simple java application

pdfmanager.java :

public class pdfManager {

private PDFParser parser;
   private PDFTextStripper pdfStripper;
   private PDDocument pdDoc ;
   private COSDocument cosDoc ;

   private String Text ;
   private String filePath;
   private File file;

    public pdfManager() {

    }
   public String ToText() throws IOException
   {
       this.pdfStripper = null;
       this.pdDoc = null;
       this.cosDoc = null;

       file = new File(filePath);
       parser = new PDFParser(new RandomAccessFile(file,"r")); // update for PDFBox V 2.0

       parser.parse();
       cosDoc = parser.getDocument();
       pdfStripper = new PDFTextStripper();
       pdDoc = new PDDocument(cosDoc);
       pdDoc.getNumberOfPages();
       pdfStripper.setStartPage(1);
       pdfStripper.setEndPage(10);

       // reading text from page 1 to 10
       // if you want to get text from full pdf file use this code
       // pdfStripper.setEndPage(pdDoc.getNumberOfPages());

       Text = pdfStripper.getText(pdDoc);
       return Text;
   }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }


}

the srvlet file :

    PrintWriter out = response.getWriter() ; 
    out.println("\ndata we gottoo : ") ; 



    pdfManager pdfManager = new pdfManager();
     pdfManager.setFilePath("/Users/rami/Desktop/pdf2.pdf");
        System.out.println(pdfManager.ToText());

called in doGet method

Rami Osman
  • 137
  • 1
  • 13

1 Answers1

1

The library you need is not on the classpath or other problems occur when the classloader wants to load the class of the library. If you are in on a server, be sure to add the library to classpath folder. This can be done by hand or your application has to provide/deliver it by itself. Since it's not clear how your app is deployed or delivered it can have many reasons

Henning Luther
  • 2,067
  • 15
  • 22
  • my project include the pdfbox library (build path -external jar) but I need to install it on the server first ? – Rami Osman Feb 27 '16 at 19:54
  • You mean your eclipse IDE includes this jar!? Your artifact which is delivered to the server to run there does not, so there are different ways to make it run. The cleanest is to package your app in the right way including this lib, the easiest is to put it in the servers lib folder – Henning Luther Feb 27 '16 at 19:58
  • I installed the classpath it works ! thanks for your help :) – Rami Osman Feb 27 '16 at 20:54