复制 PDFLib 块布局


Copy PDFLib Block Layout

我有一个pdf文件,上面有PDFlib块。这些块都没有被填满。我需要将这些块(而不是页面内容)复制到一组其他 pdf 文档中,每个文档都有自己的内容。块不需要填充。

这个想法是每个文档都是一个表单,但每个文档略有不同。表单字段都位于同一位置,因此我们只需要复制块,包括它们的布局。我已经查看了此文档页面,该页面描述了在看似单个文档上执行此操作,该文档具有新(空白)文档的块。

任何帮助,不胜感激。

我编写了一个PHP脚本来处理这个问题。见下文:

try{
        # Create a new PDFLib instance
        $pdf = new PDFlib();
        $pdf->set_option('errorpolicy=return');
        $pdf->set_option('stringformat=utf8');
        # Create the optput document
        if(!($pdf->begin_document('','')))
            throw new Exception('Error: '.$pdf->get_errmsg());
        # Open the input document, in this case the block template file
        $block_file = $pdf->open_pdi_document('block_template.pdf','');
        if($block_file == 0) throw new Exception('Error: '.$pdf->get_errmsg());
        # Also open the target file we need to copy blocks to
        $target_file = $pdf->open_pdi_document('target_file.pdf','');
        if($target_file == 0) throw new Exception('Error: '.$pdf->get_errmsg());
        # Find the number of pages in the document
        $pages = $pdf->pcos_get_number($block_file,'length:pages');
        # Loop over each page
        for($page=0;$page<$pages;$page++){
            # Open the target file to clone its contents
            $target_input_page = $pdf->open_pdi_page($target_file,1,'');
            if($target_input_page == 0) throw new Exception('Error: '.$pdf->get_errmsg());
            # Create a blank PDF page with a dummy size
            $pdf->begin_page_ext(10,10,'');
            # Copy the contents of the input page to the new page. This also overrides the size set by the dummy page
            $pdf->fit_pdi_page($target_input_page,0,0,'adjustpage');
            # Count the blocks on this template file page
            $block_count = (int) $pdf->pcos_get_number($block_file, 'length:pages['.$page.']/blocks');
            # Loop over the blocks and copy them
            for($i=0;$i<$block_count;$i++){
                $block_name = $pdf->pcos_get_string($block_file,'pages['.$page.']/blocks['.$i.'].key');
                if ($pdf->process_pdi($block_file,0,'action=copyblock block={pagenumber='.($page+1).' blockname={'.$block_name.'}}') == 0)
                    throw new Exception("Error: " . $pdf->get_errmsg());
            }
            # End this page
            $pdf->end_page_ext('');
            # Close the pdf page
            $pdf->close_pdi_page($target_input_page);
        }
        # Close the PDF document
        $pdf->close_pdi_document($block_file);
        $pdf->close_pdi_document($target_file);
        $pdf->end_document('');
        # Get the output buffer
        $buffer = $pdf->get_buffer();
        # Write out the converted file
        $output_file = fopen('output_file.pdf','w+');
        fwrite($output_file,$buffer);
        fclose($output_file);
    }
    catch(PDFlibException $e){
        error_log("PDFlib exception occurred:'n" .
            "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
            $e->get_errmsg() . "'n");
        exit(1);
    }
    catch(Exception $e){
        error_log($e);
        exit(1);
    }
在您

粘贴的示例中,您还可以导入目标 PDF,并在放置复制的块之前将其调整到页面。

另一方面,您可以使用 Acrobat 块插件并以交互方式将块从资源 PDF 复制到目标 PDF。在块插件中执行此操作的好处是,您可以直接执行此小附加项。

最后一个想法:

  • 将所有块放在空白 PDF 上。
  • 将表单导入新的 PDF
  • 导入块模板,但我们选择"盲"。因此,您可以解决这些块,但不放置任何视觉内容。

此技术还通过将块内容放置在内容下方来使用。