33namespace Fruitcake \WeasyPrint ;
44
55use Illuminate \Http \Response ;
6+ use Illuminate \Support \Facades \File ;
67use Illuminate \Support \Facades \View ;
78use Illuminate \Contracts \Support \Renderable ;
89use Pontedilana \PhpWeasyPrint \Pdf ;
@@ -65,6 +66,34 @@ public function setTemporaryFolder($path)
6566 return $ this ;
6667 }
6768
69+ /**
70+ * Set the paper size
71+ *
72+ * @param string $paper
73+ * @param string $orientation
74+ * @return $this
75+ */
76+ public function setPaper ($ paper , $ orientation = null )
77+ {
78+ $ this ->options ['page-size ' ] = $ paper ;
79+ if ($ orientation ) {
80+ $ this ->options ['orientation ' ] = $ orientation ;
81+ }
82+ return $ this ;
83+ }
84+
85+ /**
86+ * Set the orientation (default portrait)
87+ *
88+ * @param string $orientation
89+ * @return $this
90+ */
91+ public function setOrientation ($ orientation )
92+ {
93+ $ this ->options ['orientation ' ] = $ orientation ;
94+ return $ this ;
95+ }
96+
6897 /**
6998 * @param string $name
7099 * @param mixed $value
@@ -133,6 +162,25 @@ public function loadView($view, $data = array(), $mergeData = array())
133162 return $ this ->loadHTML ($ view );
134163 }
135164
165+ /**
166+ * Parse page options to stylesheet
167+ * @return array|array[]
168+ */
169+ protected function prepareOptions ()
170+ {
171+ $ options = $ this ->options ;
172+
173+ $ pageSize = $ options ['page-size ' ] ?? null ;
174+ $ orientation = $ options ['orientation ' ] ?? null ;
175+
176+ if (!$ pageSize && !$ orientation ) {
177+ return [];
178+ }
179+
180+ return [
181+ 'stylesheet ' => ["@page{ size: {$ pageSize } {$ orientation }; } " ],
182+ ];
183+ }
136184 /**
137185 * Output the PDF as a string.
138186 *
@@ -142,11 +190,11 @@ public function loadView($view, $data = array(), $mergeData = array())
142190 public function output ()
143191 {
144192 if ($ this ->html ) {
145- return $ this ->weasy ->getOutputFromHtml ($ this ->html , $ this ->options );
193+ return $ this ->weasy ->getOutputFromHtml ($ this ->html , $ this ->prepareOptions () );
146194 }
147195
148196 if ($ this ->file ) {
149- return $ this ->weasy ->getOutput ($ this ->file , $ this ->options );
197+ return $ this ->weasy ->getOutput ($ this ->file , $ this ->prepareOptions () );
150198 }
151199
152200 throw new \InvalidArgumentException ('PDF Generator requires a html or file in order to produce output. ' );
@@ -162,9 +210,9 @@ public function save($filename, $overwrite = false)
162210 {
163211
164212 if ($ this ->html ) {
165- $ this ->weasy ->generateFromHtml ($ this ->html , $ filename , $ this ->options , $ overwrite );
213+ $ this ->weasy ->generateFromHtml ($ this ->html , $ filename , $ this ->prepareOptions () , $ overwrite );
166214 } elseif ($ this ->file ) {
167- $ this ->weasy ->generate ($ this ->file , $ filename , $ this ->options , $ overwrite );
215+ $ this ->weasy ->generate ($ this ->file , $ filename , $ this ->prepareOptions () , $ overwrite );
168216 }
169217
170218 return $ this ;
0 commit comments