Html to convert pdf save in to folder and Mail

Html to convert pdf save in to folder and Mail

Here Example of html table structure to convert to pdf using jspdf.

<table id= “xyz”><thead><tr><th>id</th></tr></thead>
<tbody><tr><td>1</td></tbody></table>

You can add style tag inline and give css to it.

Thereafter Include require Js file

https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
http://html2canvas.js
http://from_html.js
http://jspdf.plugin.autotable.js

var doc = new jsPDF(‘l’, ‘pt’, ‘a4’);
var specialElementHandlers = {
‘#xyz’: function(element, renderer){
return true;
}};
var table = document.getElementById(“xyz”);
var res = doc.autoTableHtmlToJson(table, true);
doc.autoTable(res.columns, res.data, {
startY: 60,
tableWidth: ‘auto’,
columnWidth: ‘auto’,
styles: {
overflow: ‘linebreak’
},
beforePageContent: function(data) {
doc.text(“New Pdf”, 40, 30);}
});
var pdf =doc.output(); //returns raw body of resulting PDF returned as a string as per the plugin documentation.
var data = new FormData();
data.append(“data” , pdf);
var xhr = new XMLHttpRequest();
xhr.open( ‘post’, ‘test.php’, true ); //Post to php Script to save to server
xhr.send(data);

After That Create new php file to post data on it and using phpmailer you can attached and mail. eg:test.php

<?php
require_once “../../PHPMailer/class.phpmailer.php”; // Download phpmailer
if(!empty($_POST[‘data’])){
$data = $_POST[‘data’];
$fname = ‘newpdf-‘.date(‘d-m-Y’).’.pdf’; // name the file
$file = fopen(“../pdf/” .$fname, ‘w’); // open the file path
fwrite($file, $data); //save data
fclose($file);
$bodytext = ‘Please Find PDF’;
$email = new PHPMailer();
$email->From = ‘xxx@xyz.com’;
$email->FromName = ‘My first pdf’;
$email->Subject = $file;
$email->Body = $bodytext;
$email->AddAddress( ‘xyz@abc.com’ );
//$email->AddAddress(‘person2@domain.com’, ‘Person Two’);
//$file_to_attach = $_SERVER[‘DOCUMENT_ROOT’].’/front/pdf’;
$email->AddAttachment(‘../pdf/’.$fname );

return $email->Send();
} else {
echo “No Data Sent”;
}
?>