当使用 FPDI 扩展 TCPDF 时,有没有办法使用 TCPDF 中的事务?

发布于 2024-08-27 07:08:09 字数 730 浏览 4 评论 0原文

我正在将 TCPDF 与 FPDI 的桥一起使用。我遇到的问题是,一旦我使用 startTransaction() 我就会收到以下错误:

TCPDF ERROR: Cannot access protected property FPDI:$numpages / Undefined property: FPDI::$numpages

并且脚本结束(因为 TCPDF::Error() 方法中的模具)。

这是我正在使用的代码:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->startTransaction();
$pdf->Cell(0, 0, 'blah blah blah');
$pdf->rollbackTransaction();

$pdf->Output( . time() . '.pdf', 'D');

如果我将其更改为:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->Cell(0, 0, 'blah blah blah');

$pdf->Output( . time() . '.pdf', 'D');

它工作正常。

有没有办法让他们一起工作并使用 TCPDF 的交易?

I am using TCPDF with FPDI's bridge. The issue I'm having is that as soon as I use the startTransaction() I get the following error:

TCPDF ERROR: Cannot access protected property FPDI:$numpages / Undefined property: FPDI::$numpages

and the script ends (because of the die in the TCPDF::Error() method).

Here is the code I'm using:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->startTransaction();
$pdf->Cell(0, 0, 'blah blah blah');
$pdf->rollbackTransaction();

$pdf->Output( . time() . '.pdf', 'D');

If I change it to:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->Cell(0, 0, 'blah blah blah');

$pdf->Output( . time() . '.pdf', 'D');

it works fine.

Is there anyway to make them work together and use TCPDF's transactions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽蝶幻影 2024-09-03 07:08:09

我找到的解决方案是使用 PHP 的对象克隆,它允许我执行事务并在需要时将其回滚。下面是一个示例:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->Cell(0, 0, 'blah blah blah');

$_pdf = clone $pdf;

// do stuff that you may want to revert
$pdf->Cell(0, 0, 'PDFs suck!');

// revert the PDF
$pdf = $_pdf;

$pdf->Output( . time() . '.pdf', 'D');

PDF 将仅包含“blah blah blah”。

The solution I found was to to use PHP's object cloning which allows me to do transactions and roll them back whenever I want. Here's an example:

$pdf = new FPDI();

// add a page
$pdf->AddPage();

$pdf->Cell(0, 0, 'blah blah blah');

$_pdf = clone $pdf;

// do stuff that you may want to revert
$pdf->Cell(0, 0, 'PDFs suck!');

// revert the PDF
$pdf = $_pdf;

$pdf->Output( . time() . '.pdf', 'D');

The PDF will only contain "blah blah blah".

感受沵的脚步 2024-09-03 07:08:09

在第一个示例中,您应该使用 $pdf = $pdf->rollbackTransaction$pdf->rollbackTransaction(true) 而不仅仅是 $pdf-> ;rollabackTransaction()

这是因为 rollbackTransaction 采用布尔参数(默认为 false),以了解是否必须返回回滚值(false)或设置对象进入回滚状态(true)。

in your first example you should use $pdf = $pdf->rollbackTransaction or $pdf->rollbackTransaction(true) instead of just $pdf->rollabackTransaction()

this is because rollbackTransaction takes a boolean parameter (default is false), to know if it have to return the rollbackvalue (false) or set the object to the rollback state (true).

另类 2024-09-03 07:08:09
$pdf = new FPDI(); 

$pdf->AddPage();

$pdf->startTransaction(true);

$pdf->Cell(0, 0, 'blah blah blah');

$pdf->rollbackTransaction(true);

$pdf->Output( . time() . '.pdf', 'D');

在事务方法调用中添加 true 作为参数解决了我的问题。

$pdf = new FPDI(); 

$pdf->AddPage();

$pdf->startTransaction(true);

$pdf->Cell(0, 0, 'blah blah blah');

$pdf->rollbackTransaction(true);

$pdf->Output( . time() . '.pdf', 'D');

Adding true as parameter in the Transaction Method Calls solved the Problem for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文