title: How to read PDF from the .jar file
body: <p>In my maven project I have PDF file which is located inside <code>resources</code> folder. My function reads the PDF file from the <code>resources</code> folder and adds some values in the document based on the user's data. </p>
  
  <p>This project is packed as <code>.jar</code> file using <code>mvn clean install</code> and is used as dependency in my other spring boot application.</p>
  
  <p>In my spring boot project I create instace of the class that will perform some work on the PDF. Once all job on the PDF file is done, and when PDF file is saved on file system it is always empty (all pages are blank). I have impression that <code>mvn clean install</code> does something with the PDF file. Here is what I've tried so far: </p>
  
  <h1>First way</h1>
  
  <pre><code>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  File file= new ClassPathResource("/pdfs/testpdf.pdf").getFile();//Try to get PDF file
  
  PDDocument pdf = PDDocument.load(file);//Load PDF document from the file
  List&lt;PDField&gt; fields = forms.getFields();//Get input fields that I want to update in the PDF    
  fieldsMap.forEach(throwingConsumerWrapper((field,value) -&gt; changeField(fields,field,value)));//Set input field values
  
  pdf.save(byteArrayOutputStream);//Save value to the byte array
  
  </code></pre>
  
  <p>This works great, but as soon as project is packed in a <code>.jar</code> file then I get exception that <code>new ClassPathResource("/pdfs/testpdf.pdf").getFile();</code> can't find the specified file. </p>
  
  <p>This is normal because the <code>File</code> class can't access anything inside <code>.jar</code> file (it can access the <code>.jar</code> file itself only) and that is clear. </p>
  
  <p>So, the solution to that problem is to use the <code>InputStream</code> instead of the <code>File</code>. Here is what I did: </p>
  
  <h1>Second way</h1>
  
  <pre><code>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  InputStream inputStream = new ClassPathResource("/pdfs/testpdf.pdf").getInputStream();//Try to get input stream
  
  PDDocument pdf = PDDocument.load(inputStream );//Load PDF document from the input stream
  List&lt;PDField&gt; fields = forms.getFields();//Get input fields that I want to update in the PDF    
  fieldsMap.forEach(throwingConsumerWrapper((field,value) -&gt; changeField(fields,field,value)));//Set input field values
  
  pdf.save(byteArrayOutputStream);//Save value to the byte array
  
  </code></pre>
  
  <p>This time <code>getInputStream()</code> doesn't throw error and <code>inputStream</code> object is not <code>null</code>. But the PDF file once saved on my file system is empty, meaning all pages are empty.</p>
  
  <p>I even tried to copy complete <code>inputStream</code> and saving it to the file byte by byte but what I've noticed that every byte is equal 0. Here is what I did:</p>
  
  <h1>Third way</h1>
  
  <pre><code>InputStream inputStream = new ClassPathResource("/pdfs/test.pdf").getInputStream();
  byte[] buffer = new byte[inputStream.available()];
  inputStream.read(buffer);
  
  File targetFile = new File(OUTPUT_FOLDER);
  OutputStream outStream = new FileOutputStream(targetFile);
  outStream.write(buffer);
  
  </code></pre>
  
  <p>Copied <code>test.pdf</code> is saved but when opened with Adobe Reader is reported as corrupted.</p>
  
  <p>Anyone have idea how to fix this?</p>