Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic PDF height using mPDF engine #10

Open
alexmigf opened this issue Jan 10, 2023 · 0 comments
Open

Dynamic PDF height using mPDF engine #10

alexmigf opened this issue Jan 10, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@alexmigf
Copy link
Member

alexmigf commented Jan 10, 2023

A customer asked us to support dynamic height for the mPDF extension. This is required when printing PDF on a thermal paper roll printer.

We should follow the same procedure that we are doing with DOMPDF here, providing a filter and a code snippet to allow this, instead of including it directly in the plugin.

The PR attached to this issue includes a filter that can be used to dynamically set the PDF height. Here is the code snippet to set the paper width to 62:

add_filter( 'wpo_wcpdf_paper_format', function( $paper_format, $document_type ){
	if ( $document_type == 'packing-slip' ){
		$paper_format = [ 62, 600 ];
	}
	return $paper_format;
}, 10, 2 );

add_action( 'wpo_wcpdf_custom_styles', 'wpo_wcpdf_mpdf_styles', 99, 2 );
function wpo_wcpdf_mpdf_styles ( $document_type, $document ) {
	if ( $document_type == 'packing-slip' ){
		?>
		@page {
			margin-top: 1cm;
			margin-bottom: 1cm;
			margin-left: 0.25cm;
			margin-right: 0.25cm;
		}
		.order-details .total {
			width: 25%;
		}
		.order-details .quantity {
			width: 16%;
		}
		<?php
	}
}

add_filter( 'wpo_wcpdf_after_mpdf_write', function( $mpdf, $html, $options, $document ) {
	if ( $document->get_type() == 'packing-slip' ) {
		$height = $mpdf->y;
		unset( $mpdf );
		$mpdf = new \Mpdf\Mpdf( $options );
		$mpdf->_setPageSize( [ 62, ceil( $height ) + 10 ], $options['orientation'] );
		$mpdf->WriteHTML( $html );
		$mpdf->page = 1;
	}
	return $mpdf;
}, 10, 4 );
@alexmigf alexmigf self-assigned this Jan 10, 2023
@alexmigf alexmigf added the enhancement New feature or request label Jan 10, 2023
@alexmigf alexmigf pinned this issue Jan 10, 2023
@alexmigf alexmigf removed their assignment Jan 17, 2023
@alexmigf alexmigf changed the title Dynamic height for paper roll printers Dynamic PDF height using mPDF engine Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant