I have based from my code from https://github.com/eKoopmans/html2pdf.js/issues/181 (odedta) comment and also tried the second users comment below theirs.
I have a script using html2pdf bundle. Which works well. It create a multi page PDF with images. The content of the PDF is data which is pulled from a WordPress category (using a wp Query) and results in 3-6 Posts being put into a PDF.
HTML for example:
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
<div id="PDFcontent">
<h1> PDF tile </h1>
<img src="https://styles.redditmedia.com/t5_2r5i1/styles/communityIcon_x4lqmqzu1hi81.jpg?width=256&s=fc15e67e2b431bbd2e93e980be3090306b78be55">
<div class="html2pdf__page-break"></div>
<h2 class="exhibitied-title"> Page 2 title<h2>
Some text
<div class="html2pdf__page-break"></div>
<h2> Page 3 title<h2>
Some text
</div>
I have my Jquery at the bottom of the page
jQuery(function($) {
window.onload = function() {
let el = document.getElementById('PDFcontent');
var title = $('.exhibitied-title').text();
let opt = {
margin: 0.2,
filename: title + '.pdf',
image: {
type: 'jpeg',
quality: 0.99
},
html2canvas: {
scale: 2,
logging: true,
dpi: 192,
letterRendering: true,
/* useCORS: true*/
},
jsPDF: {
unit: 'mm',
format: 'a4',
orientation: 'portrait'
}
};
html2pdf().set(opt).from(el).toPdf().output('datauristring').save().then(function(pdfAsString) {
title = $('.exhibitied-title').text();
filetitle = title + '.pdf';
let data = {
'action': 'send_email_with_pdf',
'fileDataURI': pdfAsString,
'filetitle': filetitle,
}
$.post(myAjax.ajaxurl, data, function(response) {
console.log(response);
});
});
}
});
.save works perfectly, the PDF is formatted exactly how I want it.
But when it comes to saving or emailing the PDF I am having problems. The email is does send and a PDF is created. However the pdf in the email is 115bytes and just says "Failed to load PDF document." when you try opening it, and the pdf which is saved in my WP uploads folder is 0 bytes and also "Failed to load PDF document." shows.
Inside my functions.php I have
function send_email_with_pdf() {
$pdfdoc = $_POST['fileDataURI'];
$b64file = trim( str_replace( 'data:application/pdf;base64,', '', $pdfdoc ) );
$b64file = str_replace( ' ', '+', $b64file );
$decoded_pdf = base64_decode( $b64file );
//file_put_contents( $attachment, $decodPdf );
$upload_dir = wp_upload_dir();
$image_data = file_get_contents( $decoded_pdf );
$filename = $_POST['filetitle'];
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents( $file, $image_data );
$mail = new PHPMailer;
$mail->setFrom( 'me@myemail.com', 'My name' );
$mail->addAddress( 'you@youremail.com', 'your name' );
$mail->Subject = 'Subject';
$mail->Body = $filename;
$mail->addStringAttachment($decoded_pdf, $filename);
$mail->isHTML( false );
if( !$mail->send() ) {
$response = 'Message was not sent.';
$response .= 'Mailer error: ' . $mail->ErrorInfo;
}
else {
$response = 'Message has been sent.';
}
wp_send_json( $response );
}
add_action( 'wp_ajax_send_email_with_pdf', 'send_email_with_pdf' );
add_action( 'wp_ajax_nopriv_send_email_with_pdf', 'send_email_with_pdf' );
Is anyone able to point me in the right direction please, or has anyone come across this issue before?
I have posted as much information as I can, but please let me know if you need more information.