How do you combine PDFs in ruby?

前端 未结 8 2080
你的背包
你的背包 2020-12-25 15:17

This was asked in 2008. Hopefully there\'s a better answer now.

How can you combine PDFs in ruby?

I\'m using the pdf-stamper gem to fill out a form in a PDF.

8条回答
  •  自闭症患者
    2020-12-25 15:37

    Via iText, this will work... though you should flatten the forms before you merge them to avoid field name conflicts. That or rename the fields one page at a time.

    Within PDF, fields with the same name share a value. This is usually not the desired behavior, though it comes in handy from time to time.

    Something along the lines of (in java):

    PdfCopy mergedPDF = new PdfCopy( new Document(), new FileOutputStream( outPath );
    
    for (String path : paths ) {
      PdfReader reader = new PdfReader( path );
      ByteArrayOutputStream curFormOut = new ByteArrayOutputStream();
      PdfStamper stamper = new PdfStamper( reader, curFormOut );
    
      stamper.setField( name, value ); // ad nauseum
    
      stamper.setFlattening(true); // flattening setting only takes effect during close()
      stamper.close();
    
      byte curFormBytes = curFormOut.toByteArray();
      PdfReader combineMe = new PdfReader( curFormBytes );
    
      int pages = combineMe .getNumberOfPages();
      for (int i = 1; i <= pages; ++i) { // "1" is the first page
        mergedForms.addPage( mergedForms.getImportedPage( combineMe, i );
      }
    }
    
    mergedForms.close();
    

提交回复
热议问题