【问题标题】:perl: Creating PDF Avery lables on linux?perl:在 Linux 上创建 PDF Avery 标签?
【发布时间】:2015-01-08 07:36:26
【问题描述】:

在使用 perl 的 Linux 机器上创建格式化的 Avery 标签 PDF 的最佳和最灵活的过程是什么?

标签需要包含图像并且格式类似于 html 表格中的跨行和跨列。例如,左侧有几行文本,右侧有一个跨越文本的图像。

这些是我的想法,但如果您有其他想法,请告诉我。

  • 使用 PDF::API2 将 perl 转换为 PDF

  • perl 到 PS 用 ??? -> PS 到 PDF 用???

  • perl 到带有 CSS 格式的 HTML -> HTML 到带有 wkhtmltopdf 的 PDF

有没有人这样做并且有任何可能有帮助的指针、示例或链接?

谢谢你, ~多纳文

【问题讨论】:

    标签: html css linux perl pdf


    【解决方案1】:

    它们都是可行的选择。

    我发现 wkhtmltopdf 太耗费资源且速度太慢。如果您确实想走这条路,那么已经有现有的 html 模板可以通过快速google search 找到。

    PDF::API2 性能非常好,我在服务器系统上运行它没有任何问题。这是我用于以网格格式布置元素的示例脚本;

    #!/usr/bin/env perl
    use strict 'vars';
    
    use FindBin;
    use PDF::API2;
    
    # Min usage, expects bank.pdf to exist in same location
    render_grid_pdf(
      labels        => ['One', 'Two', 'Three', 'Four'],
    
      cell_width    => 200,
      cell_height   => 50,
    
      no_of_columns => 2,
      no_of_rows    => 2,
    );
    
    # Advanced usage
    render_grid_pdf(
      labels        => ['One', 'Two', 'Three', 'Four'],
    
      cell_width    => 200,
      cell_height   => 50,
    
      no_of_columns => 2,
      no_of_rows    => 2,
    
      font_name     => "Helvetica-Bold",
      font_size     => 12,
    
      template      => "blank.pdf",
      save_as       => "my_labels.pdf",
    
      # Manually set coordinates to start prinding
      page_offset_x => 20, # Acts as a left margin
      page_offset_y => 600,
    );
    
    
    sub render_grid_pdf {
      my %args = @_;
    
      # Print data
      my $labels   = $args{labels} || die "Labels required";
    
      # Template, outfile and labels
      my $template = $args{template}  || "$FindBin::Bin/blank.pdf";
      my $save_as  = $args{save_as}   || "$FindBin::Bin/out.pdf";
    
      # Layout Properties
      my $no_of_columns = $args{no_of_columns} || die "Number of columns required";
      my $no_of_rows    = $args{no_of_rows}    || die "Number of rows required";
    
      my $cell_width    = $args{cell_width}    || die "Cell width required";
      my $cell_height   = $args{cell_height}   || die "Cell height required";
    
      my $font_name     = $args{font_name}     || "Helvetica-Bold";
      my $font_size     = $args{font_size}     || 12;
    
      # Note: PDF::API2 uses cartesion coordinates, 0,0 being
      # bottom. left. These offsets are used to set the print
      # reference to top-left to make things easier to manage
      my $page_offset_x = $args{page_offset_x} || 0;
      my $page_offset_y = $args{page_offset_y} || $no_of_rows * $cell_height;
    
      # Open an existing PDF file as a templata
      my $pdf = PDF::API2->open("$template");
    
      # Add a built-in font to the PDF
      my $font = $pdf->corefont($font_name);
      my $page = $pdf->openpage(1);
    
      # Add some text to the page
      my $text = $page->text();
      $text->font($font, $font_size);
    
      # Print out labels
      my $current_label = 0;
      OUTERLOOP: for (my $row = 0; $row < $no_of_columns; $row++) {
        for (my $column = 0; $column < $no_of_columns; $column++) {
          # Calculate label x, y positions
          my $label_y = $page_offset_y - $row     * $cell_height;
          my $label_x = $page_offset_x + $column  * $cell_width;
    
          # Print label
          $text->translate( $label_x, $label_y );
          $text->text( $labels->[$current_label]);
    
    
          # Increment labels index
          $current_label++;
    
          # Exit condition
          if ( $current_label > scalar @{$labels}) {
            last OUTERLOOP;
          }
        }
      }
    
      # Save the PDF
      $pdf->saveas($save_as);
    }
    

    【讨论】:

    • 谢谢,太好了!您有包含图片的示例吗?
    • 你有两个选择;如果它是像徽标这样的静态图像,只需将图像添加到模板 pdf 文件中。对于动态图像,只需传入一个数组,例如labels_image = ['one.jpg', 'etc'] 并在# Print label 之后添加注释图像代码,例如my $image=$pdf-&gt;image_jpeg($labels_image-&gt;[$current_label]);$gfx-&gt;image( $image, $label_x, $label_y ); - 使用图像对象来获得你想要的。
    • 我能够在我们的系统上安装PDF::API2。你能给我建议blank.pdf 包含什么吗?
    • 从字面上看,只要打开 word 和 Save As->PDF。您决定保留在那里的任何文本、图像、页脚、徽标等都将保留在输出 pdf 中。
    • 太棒了。太感谢了。你在 freelancer.com 上吗 以防我更深入地了解这一点并完全感到困惑,我可能需要更具体的帮助。
    【解决方案2】:

    太好了,你找到了你喜欢的答案。

    另一个可能适合您也可能不适合您的选项是准备将作为标签打印为 open/libreoffice 文档的工作表,其中包含图片、布局、非变体文本......(并且可以您所有的测试都通过 open/libreoffice 运行)。

    然后:

    use OpenOffice::OODoc;
    

    然后:从数据库中读取数据

    然后:

    my $document = odfDocument( file => "$outputFilename", 
                        create => "text",
                        template_path => $myTemplateDir );
    

    然后:

    for (my $r = 0; $r < $NumOfTableRows; $r++ ) {
        for (my $c = 0; $c < $NumOfTableCols; $c++) {
              :
            $document->cellValue($theTableName, $r, $c, $someText);
            # test: was written properly ?
            my $writtenTest =  $document->cellValue($theTableName, $r, $c);
            chomp $writtenTest;
            if ($someText ne $writtenTest) {
                 :
             }
         }
      }
    

    然后:

    $document->save($outputFilename );
    
    # save (convert to) a pdf
    #   -f format; 
    #   -n no start new listener; use existing
    #   -T timeout to connect to its *OWN* listener (only); 
    #   -e exportFilterOptions
    
    `unoconv -f pdf -n  -T 60 -e PageRange=1-2  $outputFilename `;
    # I remove the open/libreoffice doc, else clutter and confusion
    `rm $outputFilename `;
    

    作为实际问题的快速概述:

    • 命名布局表
    • 将你漂亮、正确的 open/libreoffice 文档放在“/usr/local/lib/site_perl/myNewTemplateDir/”中。您将需要这个(我认为这是默认设置,但我还是将其传递为 $myTemplateDir
    • 问题:等待 Open/Libreoffice 启动的模块例程(用于 unoconv 转换器启动)不起作用 - unoconv 在他们说会后仍然无法工作。我创建虚拟 pdf,直到一个实际工作 - 存在并且具有非零大小。

    【讨论】:

    • 感谢您添加的信息。不幸的是,OpenOffice 和 OpenOffice::OODoc 都没有安装,而且安装它们的可能性很小。此外,图片/图像是动态的,并且每个都不同。在生产机器上安装 OpenOffice 很常见吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多