Create PDF with php - using mPDF

Requires PHP >= 8.0

1.) Download the files

45mb zipped, 90+mb unzipped, most of the file size is from the font files

2.) Unzip the downloaded files, and upload to your project's theme folder

3.) Create a page and wordpress page template file

Pdf 1

load the main.php file in the template (from the downloaded files)

Page Template Sample Code:
				
					<?php
/* Template Name: PDF */

include_once 'pdf/main.php';
				
			

the main.php just contains some default/sample codes, modify as needed.

Setting font

Change this in the $mpdf_settings[‘default_font’] on main.php

you can check all the available fonts here: https://mpdf.github.io/fonts-languages/available-fonts-v6.html

Pdf 2
Supported CSS

Modern CSS properties like flex or grid are not supported. Check here for list of supported CSS https://mpdf.github.io/css-stylesheets/supported-css.html

Sample Template Codes:
				
					<style>
.main {
    background: #fff;
    width: 100%;
    height: 1123px;
    border: 15px solid #fff;
}

.inner {
    background: #ebe9e6;
    height: 1093px;
}

.wrapper {
    width: 700px;
    margin: auto;
}

.cols-2 .col {
    width: 49.99%;
    float: left;
}
</style>

<div class="main">
    <div class="inner">

        <div class="wrapper">

            <div class="fullwidth">
                
                <h2>Fullwidth</h2>
                
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>

                <h4>WP Query Test:</h4>
                <?php
                $q = new WP_Query([
                    'post_type' => 'post',
                ]);
                foreach( $q->posts as $post ) {
                    pre_debug($post->post_title);
                }
                ?>

            </div>

            <div class="cols-2">

                <div class="col">
                    <h2>Cols 2 - Left</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
                </div>

                <div class="col">
                    <h2>Cols 2 - Right</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
                </div>

            </div>
        </div><!-- wrapper -->

    </div><!-- inner -->
</div><!-- main -->
				
			
Sample Output
Pdf 4

Share This

Select your desired option below to share a direct link to this page.
Your friends or family will thank you later.