bernhard Posted March 2, 2016 Share Posted March 2, 2016 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 More sharing options...
adrian Posted March 2, 2016 Share Posted March 2, 2016 What happens if you save after removeAll(), before you add the new one? Link to comment Share on other sites More sharing options...
bernhard Posted March 2, 2016 Author Share Posted March 2, 2016 all the same. already tried save('stickerpdf') and save() PS: PW 2.7.2 Link to comment Share on other sites More sharing options...
adrian Posted March 2, 2016 Share Posted March 2, 2016 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. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted March 2, 2016 Share Posted March 2, 2016 I'm always doing a save() after removeAll() as this does also prevent any naming collisions (no more -1). And I'd also suggest not using the page files path to create files in. Rather use wireTempDir(). Link to comment Share on other sites More sharing options...
bernhard Posted March 2, 2016 Author Share Posted March 2, 2016 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 More sharing options...
adrian Posted March 2, 2016 Share Posted March 2, 2016 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 1 Link to comment Share on other sites More sharing options...
bernhard Posted March 2, 2016 Author Share Posted March 2, 2016 thank you adrian, never used those configs so i was not aware of them. thanks for the adivce! will use wireTempDir here and in future 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now