Skip to content

Commit a709fd0

Browse files
committed
Added option to configure PDF export paper size
For #995
1 parent e17cdab commit a709fd0

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

.env.example.complete

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ RECYCLE_BIN_LIFETIME=30
297297
# Maximum file size, in megabytes, that can be uploaded to the system.
298298
FILE_UPLOAD_SIZE_LIMIT=50
299299

300+
# Export Page Size
301+
# Primarily used to determine page size of PDF exports.
302+
# Can be 'a4' or 'letter'.
303+
EXPORT_PAGE_SIZE=a4
304+
300305
# Allow <script> tags in page content
301306
# Note, if set to 'true' the page editor may still escape scripts.
302307
ALLOW_CONTENT_SCRIPTS=false

app/Config/dompdf.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
* Do not edit this file unless you're happy to maintain any changes yourself.
99
*/
1010

11+
$dompdfPaperSizeMap = [
12+
'a4' => 'a4',
13+
'letter' => 'letter',
14+
];
15+
1116
return [
1217

1318
'show_warnings' => false, // Throw an Exception on warnings from dompdf
@@ -150,7 +155,7 @@
150155
*
151156
* @see CPDF_Adapter::PAPER_SIZES for valid sizes ('letter', 'legal', 'A4', etc.)
152157
*/
153-
'default_paper_size' => 'a4',
158+
'default_paper_size' => $dompdfPaperSizeMap[env('EXPORT_PAGE_SIZE', 'a4')] ?? 'a4',
154159

155160
/**
156161
* The default font family.

app/Config/snappy.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
* Do not edit this file unless you're happy to maintain any changes yourself.
99
*/
1010

11+
$snappyPaperSizeMap = [
12+
'a4' => 'A4',
13+
'letter' => 'Letter',
14+
];
15+
1116
return [
1217
'pdf' => [
1318
'enabled' => true,
1419
'binary' => file_exists(base_path('wkhtmltopdf')) ? base_path('wkhtmltopdf') : env('WKHTMLTOPDF', false),
1520
'timeout' => false,
1621
'options' => [
1722
'outline' => true,
23+
'page-size' => $snappyPaperSizeMap[env('EXPORT_PAGE_SIZE', 'a4')] ?? 'A4',
1824
],
1925
'env' => [],
2026
],

tests/Unit/ConfigTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ public function test_dompdf_remote_fetching_controlled_by_allow_untrusted_server
8282
$this->checkEnvConfigResult('ALLOW_UNTRUSTED_SERVER_FETCHING', 'true', 'dompdf.defines.enable_remote', true);
8383
}
8484

85+
public function test_dompdf_paper_size_options_are_limited()
86+
{
87+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'dompdf.defines.default_paper_size', 'a4');
88+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'dompdf.defines.default_paper_size', 'letter');
89+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'dompdf.defines.default_paper_size', 'a4');
90+
}
91+
92+
public function test_snappy_paper_size_options_are_limited()
93+
{
94+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'snappy.pdf.options.page-size', 'A4');
95+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'snappy.pdf.options.page-size', 'Letter');
96+
$this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'snappy.pdf.options.page-size', 'A4');
97+
}
98+
8599
/**
86100
* Set an environment variable of the given name and value
87101
* then check the given config key to see if it matches the given result.

0 commit comments

Comments
 (0)