Skip to content

Commit

Permalink
Allows to set DOMPDF paper with auto height using the filter hook 'wp…
Browse files Browse the repository at this point in the history
…o_wcpdf_dompdf_auto_height_enable'

#157
  • Loading branch information
alexmigf committed Mar 16, 2021
1 parent ebdb606 commit 897cc05
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions includes/class-wcpdf-pdf-maker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Dompdf\Dompdf;
use Dompdf\Options;
use Dompdf\Adapter\CPDF;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand Down Expand Up @@ -46,6 +47,11 @@ public function output() {
'isFontSubsettingEnabled' => $this->settings['font_subsetting'],
) ) );

// if auto height
if( apply_filters( 'wpo_wcpdf_dompdf_auto_height_enable', __return_false() ) ) {

This comment has been minimized.

Copy link
@Spreeuw

Spreeuw Mar 16, 2021

Contributor

using just false here is more efficient

$this->settings['paper_size'] = $this->auto_height_paper_size( $options );
}

// instantiate and use the dompdf class
$dompdf = new Dompdf( $options );
$dompdf->loadHtml( $this->html );
Expand All @@ -57,6 +63,41 @@ public function output() {
return $dompdf->output();
}

private function auto_height_paper_size( $options ) {
$dompdf = new Dompdf( $options );
$dompdf->setPaper( $this->settings['paper_size'], $this->settings['paper_orientation'] );

$GLOBALS['bodyHeight'] = 0;
$dompdf->setCallbacks(
array(
'myCallbacks' => array(
'event' => 'end_frame',
'f' => function ( $infos ) {
$frame = $infos["frame"];
if ( strtolower( $frame->get_node()->nodeName ) === "body" ) {
$padding_box = $frame->get_padding_box();
$GLOBALS['bodyHeight'] += $padding_box['h'];
}
},
)
)
);

$dompdf->loadHtml( $this->html );
$dompdf->render();
unset( $dompdf );

$paper_sizes = CPDF::$PAPER_SIZES;
$settings_paper_size = strtolower( $this->settings['paper_size'] );
if( isset( $paper_sizes[$settings_paper_size] ) ) {
$paper_size = $paper_sizes[$settings_paper_size];
$paper_size[3] = $GLOBALS['bodyHeight'] + apply_filters( 'wpo_wcpdf_dompdf_auto_height_margin', 150 );
return $paper_size;
} else {
return $this->settings['paper_size'];
}
}

private function get_chroot_paths() {
$chroot = array( WP_CONTENT_DIR ); // default

Expand Down

0 comments on commit 897cc05

Please sign in to comment.