• Resolved psycoles

    (@psycoles)


    Hi,

    I would like to force the packing slip to generate only one page with the fixed Footer. It’s ok that the product list will be displayed partially but the important thing that should be generated is Header + Footer of the packing slip. So, I would like to know if there’s any function to do that?

    Currently, I have to count for an item in the order and stop render when over 10 products to preserve space for Footer.

    Let me know if you have any questions, thank you!

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

    (@yordansoares)

    Hi @psycoles,

    Maybe you only want to make sure to repeat both the header and footer in all the pages of the PDF documents? If so, please take a look at this article from the plugin’s Docs: Repeating headers & footers

    Thread Starter psycoles

    (@psycoles)

    Hi @yordansoares,

    Thank you for your answer I’m thinking about that too.

    Does that mean there is no way we can limit the packing slip to generate just only one page ?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @psycoles,

    We do not have a ready-made method to stop the PDF invoice rendering on the 1st page. However, I wonder if maybe you are looking for a method to auto resize the page height in order to keep all the info in a unique page. If so, I can provide you with a code snippet to do so.

    Thread Starter psycoles

    (@psycoles)

    Sorry for late reply, that snippet would be interesting to try.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    No problem, @psycoles!

    Here you go:

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Creates the PDF document on a single page by calculating the height of the document
     */
    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 ( \Dompdf\Frame $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 );

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

    In brief, if you have a child theme, you can add code snippets in its functions.php file: don’t add code snippets to the functions.php file from your parent theme, because you may lose your customizations after updating your parent theme! The another way to add code snippets, that is safer in my humble opinion, is to use the Code Snippets plugin: This plugin stores the code snippets in your database, so you don’t have to worry about losing your customizations after updating your plugins or theme 😉

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to check if documents has more than one page’ is closed to new replies.