Jump to content

Problem with images->removeAll() - Bug?


bernhard
 Share

Recommended Posts

hi everyone,

can you please confirm that i'm not doing anything wrong with this:

// filename for generated pdf
$filename = 'sticker' . date('ymdHis') . '.pdf';
$path = wire('config')->paths->files . $page->id . '/';
$filename = $path.$filename;

$mpdf->WriteFixedPosHTML(
    '<div style="font-size: 10pt;">TEST ' . date('H:i:s') . '</div>',
    29, 53, 67, 1
);

$mpdf->Output($filename, 'F');

$page->of(false);
$page->stickerpdf->removeAll();        
$page->stickerpdf->add($filename);
$page->save('stickerpdf');

this code works, but as soon as i change the filename to a static value (like sticker.pdf without the timestamp) after the second creation of the file (= first overwrite of an existing file) the sticker field will hold a 0 byte file with name "sticker.pdf" and i get the error

Warning: filemtime(): stat failed for /var/www/.../site/assets/files/1030/sticker.pdf in /var/www/.../wire/core/Pagefile.php on line 324
Link to comment
Share on other sites

Any chance it has to do with where you are generating the pdf? I would do it in a temp directory first - I think there is likely a conflict with saving it to the assets/files/xxxx folder of the page and then adding it to the same page.

  • Like 1
Link to comment
Share on other sites

ah, of course that was the problem! i had a save() directly after the removeAll() but that didn't make any difference. but adrian got me on the right track:

  • removeAll()
  • save()
  • createPDF()
  • add()
  • save()
// early return when no valid page object
if(!$page->id) return false;
$page->of(false);
$page->stickerpdf->removeAll();
$page->save('stickerpdf');

// get mpdf instance
$pdf = $this->modules->get('WirePDF');
$mpdf = $pdf->mpdf;

// filename for generated pdf
$filename = 'sticker.pdf';
$path = wire('config')->paths->files . $page->id . '/';
$filename = $path.$filename;

$mpdf->WriteFixedPosHTML(
    '<div style="font-size: 10pt;">TEST ' . date('H:i:s') . '</div>',
    29, 53, 67, 1
);

$mpdf->Output($filename, 'F');
$page->stickerpdf->add($filename);
$page->save('stickerpdf');

thank you both!

Link to comment
Share on other sites

Sure that will work, but I still think it's better to create the PDF in a temp directory :)

Also, just an FYI, building the path up using:

$path = wire('config')->paths->files . $page->id . '/';

is not ideal. This is better:

$page->filesManager()->path()

because it handles custom paths just in case you have $config->pagefileSecure set to true, or if you have $config->pagefileExtendedPaths true.

But still if you are using a temp dir, then this won't matter anymore :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...