title: Creating PDF file in java using PDDocument results in corrupted PDF files
body: <p>I'm trying to create temporary <code>PDF</code> files in Java using <code>PDDocument</code>. I'm employing the following method to create a temporary <code>PDF</code> file.</p>
  
  <pre><code>/* Create a temporary PDF file.*/
  private File createPdf(String fileName) throws IOException {
      final PDDocument document = new PDDocument();
      final File file = File.createTempFile(fileName, ".pdf");
      //write it
      BufferedWriter bw = new BufferedWriter(new FileWriter(file));
      bw.write("This is the temporary pdf file content");
      bw.close();
      document.save(file);        
      document.close();
      return file;
  }
  </code></pre>
  
  <p>This is the test.</p>
  
  <pre><code>@Test
  public void testCreateAndMergePdfs() throws IOException {
      Collection&lt;File&gt; pdfs = new ArrayList&lt;&gt;(Arrays.asList(createPdf("File1"), createPdf("File2")));
      assertFalse(CollectionUtils.isEmpty(pdfs));
      PdfPrintPojo pdfPrintPojo = new PdfPrintPojo(pdfs);
      File mergedFile = service.createAndMergePDFs(pdfPrintPojo, "Merged");
      assertNotNull(mergedFile);
      List&lt;File&gt; list = new ArrayList&lt;&gt;(pdfs);
      File file1 = list.get(0);
      File file2 = list.get(1);
      assertTrue(FileUtils.contentEquals(file1, file2));
  }
  </code></pre>
  
  <p>What I'm trying to do here is to create and merge two <code>PDF</code> files. When I run the test, it creates two <code>PDF</code> files in the <code>temp</code> folder, for example, <code>\AppData\Local\Temp\File16375814641476797612.pdf</code> and <code>\AppData\Local\Temp\File24102718409195239661.pdf</code> and the merged file at <code>\AppData\Local\Temp\Merged_merged_3755858389884894769.pdf</code>. But the test fails at
   <code>assertTrue(FileUtils.contentEquals(file1, file2));</code>
  When I try to open the <code>PDF</code> files in the <code>temp</code> folder, it says that the <code>PDF</code> is corrupted. Also, I have no idea why the files are not being saved as <code>File1</code> and <code>File2</code>. Can anyone help me with this? </p>