DataTables PDF Export - How to include a company logo

Adding a company logo to the top of a PDF generated by DataTables is a nice way to make the export looks more professional.

With the code snippet below and the assistance of an online tool that converts images into a base64 code, that's super easy.

First, we need go to Google and search for "base64 image encoder". Here's a link to help you with that:

There are plenty of free online solutions for that. In this example, we'll be using the first one: The solution is super easy to use; We just need to drag our company logo to the dotted area and then copy the generated code after the image is processed.

Screenshot of an online tool to convert images into base64 code

Below we see a small part of the base64 code generated from a GravityKit logo. It's usually a long text.


Now, we need to paste that code inside this code snippet where it says "REPLACE WITH BASE64 CODE". Pay attention to put it inside the single quotes.

document.addEventListener( 'DOMContentLoaded', function () {
wp.hooks.addFilter( 'gk.datatables.options', 'dt-custom-code', function ( options ) {
	options.buttons = button => {
		if ( 'pdf' === button.extend ) {
			button.customize = function ( doc ) {
				doc.content.splice( 1, 0, {
					margin: [ 0, 0, 0, 12 ],
					alignment: 'center',
                                        width: 200,
					height: 80,
					image: 'REPLACE WITH BASE64 CODE'
				} );

		return button;
	} );

	return options;
} );
} );

The resulting code will be huge, so we'll just add a link here as an example of how it should look like. Click here to see the full code.

Now we need to put all that code on the Custom JavaScript editor that sits inside View Editor > View Settings metabox > Custom Code tab.

Screenshot of the View Settings metabox

Here's how our exported PDF looks like now:

Screenshot of PDF containing a GravityKit logo

If needed, we can customize the width and height of the image by tweaking these values in the code snippet directly:

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us