anonymoushuman
anonymoushuman

Reputation: 53

Generate PDF from HTML in Wordpress and get blank PDF

I have a WordPress-site and I would like to generate a PDF with jsPDF. It works, but the PDF is blank. I have set up another Page without WordPress and there it works with the same code. The sample that I use to code is in codepen, but I'm not the author. Now my question, can it be, that jsPDF didn't work with WordPress? If yet, where is my Error?

functions.php

wp_enqueue_script( 'pdf', get_stylesheet_directory_uri() . '/generatePDF.js', array( 'jquery' ),'',true );
wp_enqueue_script( 'jsPDF', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js', array( 'jquery' ),'',true );
wp_enqueue_script( 'jsPDF-autotable', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.4/jspdf.plugin.autotable.min.js', array( 'jquery' ),'',true );on 04.03.2021

myCustomTemplate.php

<div id='a4' style='height: 297mm; width: 210mm; border: 1px black solid; margin-left:20%;'>
    <p>Content of PDF</p>
</div>

generatePDF.js

function exportPDF() { var doc = new jsPDF('p', 'mm', 'a4');

var specialElementHandlers = {
    '#getPDF': function(element, renderer){
    return true;
    },
    '.controls': function(element, renderer){
    return true;
    }
};

doc.fromHTML(jQuery('#a4').get(0), 15, 15, {
    'width': 170, 
    'elementHandlers': specialElementHandlers
});

doc.save('offerte.pdf');

}

Upvotes: 0

Views: 1296

Answers (1)

Sevicode
Sevicode

Reputation: 55

I think you also need to add this line of code before enqueue:

wp_register_script( 'jsPDF', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js');

Upvotes: 0

Related Questions