gpt4 book ai didi

html - perl:在 Linux 上创建 PDF Avery 标签?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:18 26 4
gpt4 key购买 nike

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

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

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

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

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

  • perl to HTML w/CSS formatting -> HTML to PDF with wkhtmltopdf

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

谢谢你,~多纳文

最佳答案

它们都是可行的选择。

我发现 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);
}

关于html - perl:在 Linux 上创建 PDF Avery 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26870231/

26 4 0