是否有 python ReportLab 等同于 TCPDF 的 'annotate' 函数?
Is there a python ReportLab equivalent of TCPDF's 'annotate' function?
我正在尝试使用 reportlab 库在 pdf 上编写 'annotation'。我已经使用 reportlab 成功地将新数据写入 pdf,但我找不到任何关于如何创建注释的信息。
当我说注释时,我指的是 TCPDF 的注释功能。这会在 pdf 中创建一个可点击且可移动的文本对象。
https://tcpdf.org/examples/example_036/
python一定有办法做到这一点,但到目前为止我还没有找到任何信息。
我查看了这个 post 和已接受答案中的相关链接 post。
Add text to existing PDF document in Python
我在 python 中看到过使用 php 的这个工具,但我无法让它正常工作,而且它似乎没有任何支持。
https://github.com/dnet/ppcg/blob/master/tcpdf_example.py
当我 运行 这个例子时,我得到一个不可读的 pdf,其文本如下所示:
<?
include('tcpdf/config/lang/eng.php');
include('tcpdf/tcpdf.php');
$v0 = new TCPDF('PDF_PAGE_ORIENTATION', 'PDF_UNIT', 'PDF_PAGE_FORMAT', 'true', 'UTF-8', 'false');
$v0->setFontSubsetting(False);
$v0->setAuthor('VSzA');
$v0->setCreator('PPCG');
$v0->setTitle('TCPDF in Python!?');
$v0->setPrintHeader(False);
$v0->setMargins(10, 10);
$v0->AddPage();
$v0->SetFont('dejavusans', 'B', 12);
$v0->Cell(0, 0, 'Hello Python');
$v0->Ln();
$v2 = $v0->GetX();
$v1 = $v0->GetY();
$v0->setXY(30, 30);
$v0->Cell(0, 0, 'GOTOs are bad');
$v3 = $v1 + 2.5;
$v0->setXY($v2, $v3);
$v0->Cell(0, 0, 'I can has PHP variables');
$v0->Output();
?>
这看起来像是使用 TCPDF 创建 pdf 的正确 php 代码,但代码被保存到 pdf 文件而不是 运行。
我现在得出的结论是使用 php 将我的所有数据从 python 脚本发送到 http 服务器并使用 TCPDF 在服务器上创建我的 pdf ,然后将新的 pdf 发送回我的 python 脚本以将其提供给最终用户。这听起来效率不高,所以我不想这样做。
如有任何帮助,我们将不胜感激!
-杰克
在 reportlab, the good news is it is possible to annotate in reportlab 中有关于注释的好消息和坏消息。坏消息是,这很让人头疼,因为它没有被正确记录,而且它很难限制你的选择。
您将需要的功能 canavas.textAnnotation
按以下方式使用:
canvas.textAnnotation("Your content", Rect=[x_begin, y_begin, x_end, y_end], relative=1)
这会将注释放置在相对于当前 canvas 的 (x_begin, y_begin)
处,或者如果您关闭相对于左下角的 relative
处。
您可能会注意到 Reportlab 注释看起来与 adobe 生成的注释不同,这与注释的 SubType
有关,在 Reportlab 中注释固定为 Text
而 Adobe 使用其他内容(参见 PDF reference) 的 8.4.5。
这可以通过重载 Canvas
object and the Annotation
object 来改变,但是仅仅改变一个图标就需要做很多工作。所以我不推荐它。
我正在尝试使用 reportlab 库在 pdf 上编写 'annotation'。我已经使用 reportlab 成功地将新数据写入 pdf,但我找不到任何关于如何创建注释的信息。
当我说注释时,我指的是 TCPDF 的注释功能。这会在 pdf 中创建一个可点击且可移动的文本对象。
https://tcpdf.org/examples/example_036/
python一定有办法做到这一点,但到目前为止我还没有找到任何信息。
我查看了这个 post 和已接受答案中的相关链接 post。
Add text to existing PDF document in Python
我在 python 中看到过使用 php 的这个工具,但我无法让它正常工作,而且它似乎没有任何支持。
https://github.com/dnet/ppcg/blob/master/tcpdf_example.py
当我 运行 这个例子时,我得到一个不可读的 pdf,其文本如下所示:
<?
include('tcpdf/config/lang/eng.php');
include('tcpdf/tcpdf.php');
$v0 = new TCPDF('PDF_PAGE_ORIENTATION', 'PDF_UNIT', 'PDF_PAGE_FORMAT', 'true', 'UTF-8', 'false');
$v0->setFontSubsetting(False);
$v0->setAuthor('VSzA');
$v0->setCreator('PPCG');
$v0->setTitle('TCPDF in Python!?');
$v0->setPrintHeader(False);
$v0->setMargins(10, 10);
$v0->AddPage();
$v0->SetFont('dejavusans', 'B', 12);
$v0->Cell(0, 0, 'Hello Python');
$v0->Ln();
$v2 = $v0->GetX();
$v1 = $v0->GetY();
$v0->setXY(30, 30);
$v0->Cell(0, 0, 'GOTOs are bad');
$v3 = $v1 + 2.5;
$v0->setXY($v2, $v3);
$v0->Cell(0, 0, 'I can has PHP variables');
$v0->Output();
?>
这看起来像是使用 TCPDF 创建 pdf 的正确 php 代码,但代码被保存到 pdf 文件而不是 运行。
我现在得出的结论是使用 php 将我的所有数据从 python 脚本发送到 http 服务器并使用 TCPDF 在服务器上创建我的 pdf ,然后将新的 pdf 发送回我的 python 脚本以将其提供给最终用户。这听起来效率不高,所以我不想这样做。
如有任何帮助,我们将不胜感激! -杰克
在 reportlab, the good news is it is possible to annotate in reportlab 中有关于注释的好消息和坏消息。坏消息是,这很让人头疼,因为它没有被正确记录,而且它很难限制你的选择。
您将需要的功能 canavas.textAnnotation
按以下方式使用:
canvas.textAnnotation("Your content", Rect=[x_begin, y_begin, x_end, y_end], relative=1)
这会将注释放置在相对于当前 canvas 的 (x_begin, y_begin)
处,或者如果您关闭相对于左下角的 relative
处。
您可能会注意到 Reportlab 注释看起来与 adobe 生成的注释不同,这与注释的 SubType
有关,在 Reportlab 中注释固定为 Text
而 Adobe 使用其他内容(参见 PDF reference) 的 8.4.5。
这可以通过重载 Canvas
object and the Annotation
object 来改变,但是仅仅改变一个图标就需要做很多工作。所以我不推荐它。