title: PdfDocument to byte[] using PdfMerger iText7
body: <p>I've a requirement, where I am generating different pdf' using <code>iText 7.1.11</code>. I am using <code>PdfMerger</code> to merge all pdf's on the fly. I am able to generate pdf successfully at my local system, but the application needs to send <code>bye[]</code> in response. The solution I found <a href="https://kb.itextpdf.com/home/it7kb/faq/how-can-i-serve-a-pdf-to-a-browser-without-storing-a-file-on-the-server-side" rel="nofollow noreferrer">here</a> and <a href="https://javatorch.wordpress.com/2013/09/19/generate-a-pdf-using-itext-as-a-byte-array/" rel="nofollow noreferrer">here</a> . but the problem is <code>PdfMerger</code> does not accept <code>Document</code> object, and I am not sure if i revert my code to use <code>Document</code> instead of <code>PdfDocument</code> will it work or not.
  Below is the code, with what I tried.</p>
  <pre><code> public static void createPdf(List&lt;String&gt; src, String dest, PageSize pageSize, boolean rotate, String baseUri) throws IOException {
      ConverterProperties properties = new ConverterProperties();
      properties.setBaseUri(baseUri);
      FontProvider fontProvider = new DefaultFontProvider(false,false,true);
      properties.setFontProvider(fontProvider);
      
      /** tried this to make return byte[] in response
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
      Document doc = new Document(pdfDoc); **/
  
      ///////////// Working on Local/////
      PdfWriter writer = new PdfWriter(dest); // 'dest' is local file system path 
      PdfDocument pdf = new PdfDocument(writer);
      PdfMerger merger = new PdfMerger(pdf);
  
      for (String html : src) {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          PdfDocument temp = new PdfDocument(new PdfWriter(baos));
          if(rotate) {
              temp.setDefaultPageSize(pageSize.rotate()); /** Page Size and Orientation */
          } else {
              temp.setDefaultPageSize(pageSize); /** Page Size and Orientation */
          }
          HtmlConverter.convertToPdf(html, temp, properties);
          temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
          merger.merge(temp, 1, temp.getNumberOfPages());
          temp.close();
      }
      pdf.close();}
  </code></pre>
  <p>Please help me, as this simple thing seems difficult to achieve</p>