• is there any way set pdf height based on content on the pdf. We have link pdf to printing setup. if I use width and height it print properly on my 80mm printer.
    but it make problem when item is too many on the invoice.
    I try to set width and disable height filter code but it break the pdf.

    So let me know is there anyway i can set pdf width 80mm and height auto?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @rokeyfx,

    Please add this code snippet to your site to set the PDF height of the content:

    add_filter( 'wpo_wcpdf_before_dompdf_render', function( $_dompdf, $html )
    {
    	$_dompdf_options           = $_dompdf->getOptions();
    	$_dompdf_paper_size        = $_dompdf->getPaperSize();
    	$_dompdf_paper_orientation = $_dompdf->getPaperOrientation();
    
    	$GLOBALS['wpo_wcpdf_dompdf_body_height'] = 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['wpo_wcpdf_dompdf_body_height'] += $padding_box['h'];
    					}
    				},
    			)
    		)
    	);
    
    	$_dompdf->loadHtml( $html );
    	$_dompdf->render();
    	unset( $_dompdf );
    
    	if( ! empty( $_dompdf_paper_size  ) ) {
    		$_dompdf_paper_size[3] = $GLOBALS['wpo_wcpdf_dompdf_body_height'] + 150;
    	}
    
    	$dompdf = new \Dompdf\Dompdf( $_dompdf_options );
    	$dompdf->loadHtml( $html );
    	$dompdf->setPaper( $_dompdf_paper_size, $_dompdf_paper_orientation );
    
    	return $dompdf;
    }, 10, 2 );

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

    Let me know if it works!

    Thread Starter Rokeybur Rahman

    (@rokeyfx)

    it work with auto height. but now how i set width 80mm fixed?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @rokeyfx,

    I can provide you another code snippet to set the width to 80mm, but it will require several CSS rules to fit your invoice elements to the right place and size.

    My recommendation is to use Premium Templates since this extension includes a customizer that allows you to tailor your PDF documents using product columns, total rows, and custom blocks.

    However, here is the code for you:

    add_filter( 'wpo_wcpdf_paper_format', function( $paper_format, $document_type) {
    	// change the values below
        $width = 80; //mm!
        $height = 115; //mm!
     
        //convert mm to points
        $paper_format = array( 0, 0, ($width/25.4) * 72, ($height/25.4) * 72 );
     
        return $paper_format;
    }, 10, 2 );
    
    add_filter( 'wpo_wcpdf_before_dompdf_render', function( $_dompdf, $html )
    {
    	$_dompdf_options           = $_dompdf->getOptions();
    	$_dompdf_paper_size        = $_dompdf->getPaperSize();
    	$_dompdf_paper_orientation = $_dompdf->getPaperOrientation();
    
    	$GLOBALS['wpo_wcpdf_dompdf_body_height'] = 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['wpo_wcpdf_dompdf_body_height'] += $padding_box['h'];
    					}
    				},
    			)
    		)
    	);
    
    	$_dompdf->loadHtml( $html );
    	$_dompdf->render();
    	unset( $_dompdf );
    
    	if( ! empty( $_dompdf_paper_size  ) ) {
    		$_dompdf_paper_size[3] = $GLOBALS['wpo_wcpdf_dompdf_body_height'] + 150; // if too many space at the bottom, replace this 150 value with a lower one
    	}
    
    	$dompdf = new \Dompdf\Dompdf( $_dompdf_options );
    	$dompdf->loadHtml( $html );
    	$dompdf->setPaper( $_dompdf_paper_size, $_dompdf_paper_orientation );
    
    	return $dompdf;
    }, 10, 2 );
    Thread Starter Rokeybur Rahman

    (@rokeyfx)

    Thank you.

    Thread Starter Rokeybur Rahman

    (@rokeyfx)

    Hi another request.
    is there anyway disable page break?
    so it make very long pdf.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @rokeyfx,

    Use the code that I left you above to get a unique sheet of 80?mm width per order, special for continuous feed printers.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘pdf height’ is closed to new replies.