From 020e862d329b2373eb554d2fffec4f6844d9a8b2 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 30 Oct 2025 01:36:53 +0000 Subject: [PATCH] Bug 38516: Use PDF template copy for exporting PDFs Due to a bug in PDF::API2 or related libraries, you cannot export a PDF if you do not have write permission on the PDF template. We bypass this problem by taking a string copy of the PDF template and working on that instead. Test plan: 0. DO NOT APPLY THE PATCH YET 1. chmod 444 /kohadevbox/koha/koha-tmpl/intranet-tmpl/prog/pdf/*.pdf 2. Create a basket with a vendor 3. Add an order to the basket 4. Close basket (and attach the basket to a new basket group with the same name) 5. Go to "Closed" tab and click "Export as PDF" 6. Note that you get an error when you try to open it in Adobe or some other PDF program 7. Apply the patch and restart koha (e.g. koha-plack --restart kohadev) 8. Try to "Export as PDF" again 9. Note that this time there is no warning and your PDF opens just fine 10. Repeat the "Export as PDF" process for every option of the system preference "OrderPdfFormat". Every option should work without errors. 11. Celebrate! Signed-off-by: Arthur Suzuki Signed-off-by: Marcel de Rooy Signed-off-by: Lucas Gass (cherry picked from commit a6ba625592f05fbe952f7fe8fef97b7e12eb00f7) Signed-off-by: Paul Derscheid --- Koha/pdfformat/layout1page.pm | 4 +++- Koha/pdfformat/layout2pages.pm | 4 +++- Koha/pdfformat/layout2pagesde.pm | 4 +++- Koha/pdfformat/layout3pages.pm | 4 +++- Koha/pdfformat/layout3pagesfr.pm | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Koha/pdfformat/layout1page.pm b/Koha/pdfformat/layout1page.pm index b7e42ec6ef..c8117cbdf6 100644 --- a/Koha/pdfformat/layout1page.pm +++ b/Koha/pdfformat/layout1page.pm @@ -249,7 +249,9 @@ sub printpdf { # open the default PDF that will be used for base (1st page already filled) my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout1page.pdf'; - my $pdf = PDF::API2->open($pdf_template); + my $pdf_template_obj = PDF::API2->open($pdf_template); + my $pdf_template_str = $pdf_template_obj->to_string(); + my $pdf = PDF::API2->from_string($pdf_template_str); $pdf->pageLabel( 0, { diff --git a/Koha/pdfformat/layout2pages.pm b/Koha/pdfformat/layout2pages.pm index ef790ecfc4..499a0b0606 100644 --- a/Koha/pdfformat/layout2pages.pm +++ b/Koha/pdfformat/layout2pages.pm @@ -264,7 +264,9 @@ sub printpdf { # open the default PDF that will be used for base (1st page already filled) my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pages.pdf'; - my $pdf = PDF::API2->open($pdf_template); + my $pdf_template_obj = PDF::API2->open($pdf_template); + my $pdf_template_str = $pdf_template_obj->to_string(); + my $pdf = PDF::API2->from_string($pdf_template_str); $pdf->pageLabel( 0, { diff --git a/Koha/pdfformat/layout2pagesde.pm b/Koha/pdfformat/layout2pagesde.pm index 3368446dd1..5a3e15c7fb 100644 --- a/Koha/pdfformat/layout2pagesde.pm +++ b/Koha/pdfformat/layout2pagesde.pm @@ -266,7 +266,9 @@ sub printpdf { # open the default PDF that will be used for base (1st page already filled) my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout2pagesde.pdf'; - my $pdf = PDF::API2->open($pdf_template); + my $pdf_template_obj = PDF::API2->open($pdf_template); + my $pdf_template_str = $pdf_template_obj->to_string(); + my $pdf = PDF::API2->from_string($pdf_template_str); $pdf->pageLabel( 0, { diff --git a/Koha/pdfformat/layout3pages.pm b/Koha/pdfformat/layout3pages.pm index 4721d1a4df..e34d78ebd2 100644 --- a/Koha/pdfformat/layout3pages.pm +++ b/Koha/pdfformat/layout3pages.pm @@ -455,7 +455,9 @@ sub printpdf { # open the default PDF that will be used for base (1st page already filled) my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pages.pdf'; - my $pdf = PDF::API2->open($pdf_template); + my $pdf_template_obj = PDF::API2->open($pdf_template); + my $pdf_template_str = $pdf_template_obj->to_string(); + my $pdf = PDF::API2->from_string($pdf_template_str); $pdf->pageLabel( 0, { diff --git a/Koha/pdfformat/layout3pagesfr.pm b/Koha/pdfformat/layout3pagesfr.pm index cd6652ce15..a3d9f8a01b 100644 --- a/Koha/pdfformat/layout3pagesfr.pm +++ b/Koha/pdfformat/layout3pagesfr.pm @@ -449,7 +449,9 @@ sub printpdf { # open the default PDF that will be used for base (1st page already filled) my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pagesfr.pdf'; - my $pdf = PDF::API2->open($pdf_template); + my $pdf_template_obj = PDF::API2->open($pdf_template); + my $pdf_template_str = $pdf_template_obj->to_string(); + my $pdf = PDF::API2->from_string($pdf_template_str); $pdf->pageLabel( 0, {