Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 复制PDFLib块布局_Php_Pdflib - Fatal编程技术网

Php 复制PDFLib块布局

Php 复制PDFLib块布局,php,pdflib,Php,Pdflib,我有一个pdf文件,上面有PDFlib块。这些区块都没有被填满。我需要将这些块(而不是页面内容)复制到一组其他pdf文档上,每个文档都有自己的内容。这些块不需要填充 每个文档都是一个表单,但每个文档都略有不同。表单字段都在同一位置,所以我们只需要复制块,包括它们的布局。我已经签出了,它描述了在似乎是一个单独的文档上执行此操作,并将块添加到一个新的(空白)文档中 感谢您的帮助 在粘贴的示例中,您还可以导入目标PDF,并在放置复制的块之前将其适配到页面 另一方面,您可以使用Acrobat块插件,以交

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

每个文档都是一个表单,但每个文档都略有不同。表单字段都在同一位置,所以我们只需要复制块,包括它们的布局。我已经签出了,它描述了在似乎是一个单独的文档上执行此操作,并将块添加到一个新的(空白)文档中


感谢您的帮助

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

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

最后一个想法:

  • 把你所有的区块都放在一张空白的PDF上
  • 将表单导入到新的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);
    }
试试看{
#创建新的PDFLib实例
$pdf=新的PDFlib();
$pdf->set_选项('errorpolicy=return');
$pdf->set_选项('stringformat=utf8');
#创建optput文档
如果(!($pdf->begin_文档('','))
抛出新异常('Error:'。$pdf->get_errmsg());
#打开输入文档,在本例中为块模板文件
$block_file=$pdf->open_pdi_文档('block_template.pdf','');
如果($block_file==0)抛出新异常('Error:'。$pdf->get_errmsg());
#同时打开我们需要将块复制到的目标文件
$target_file=$pdf->open_pdi_文档('target_file.pdf','');
如果($target_file==0)抛出新异常('Error:'。$pdf->get_errmsg());
#查找文档中的页数
$pages=$pdf->pcos_get_编号($block_文件,'length:pages');
#在每一页上循环
对于($page=0;$pageopen_pdi_页面($target_文件,1,”);
如果($target_input_page==0)抛出新异常('Error:'。$pdf->get_errmsg());
#创建具有虚拟大小的空白PDF页面
$pdf->begin_page_ext(10,10');
#将输入页面的内容复制到新页面。这也会覆盖虚拟页面设置的大小
$pdf->fit_pdi_页面($target_input_页面,0,0,'adjustpage');
#计算此模板文件页上的块数
$block_count=(int)$pdf->pcos_get_number($block_file,'length:pages['.$page.]/blocks');
#在块上循环并复制它们
对于($i=0;$ipcos_get_字符串($block_文件,'pages['.$page.]]/blocks['.$i.].key');
如果($pdf->process_pdi($block_file,0,'action=copyblock block={pagenumber='。($page+1)。'blockname={.$block_name.}}')==0)
抛出新异常(“错误:”..pdf->get_errmsg());
}
#结束本页
$pdf->end_page_ext(“”);
#关闭pdf页面
$pdf->close_pdi_页面($target_input_页面);
}
#关闭PDF文档
$pdf->close_pdi_文档($block_文件);
$pdf->close_pdi_文档($target_文件);
$pdf->end_文件(“”);
#获取输出缓冲区
$buffer=$pdf->get_buffer();
#写出转换后的文件
$output_file=fopen('output_file.pdf','w+');
fwrite($output_file,$buffer);
fclose($output_file);
}
捕获(PDFlibException$e){
错误\u日志(“发生PDFlib异常:\n”。
“[”$e->get_errnum()。“]”“][”$e->get_apiname()。:”。
$e->get_errmsg()。“\n”);
出口(1);
}
捕获(例外$e){
错误日志($e);
出口(1);
}