Saving PDF file on local machine is easy - but if you want to save it on server location - then we have two waays ..
1. Using JavaScript AJAX call ...
2. Using PHP package - wkhtmltopdf
=====================1. JavaScript Way -============================
pdfmaker.php or .html
<div id="kil">
<!-- your can use URL or path for image -->
<img id="kil2" src="../folder/vasu.png" >
<p>
Kilvish is the Biggest villan of the Earth Planet.
</p>
</div>
<!-- Include the jsPDF library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
window.jsPDF = window.jspdf.jsPDF;
// Create a new jsPDF instance
var doc = new jsPDF();
// Get the HTML element you want to capture
var element = document.getElementById('kil');
// Use html2canvas library to capture the screen data
html2canvas(element).then(function(canvas) {
var img2 = document.getElementById('kil2');
// Convert canvas to base64 image data
var imgData = canvas.toDataURL('image/png');
doc.addImage(img2, 'png', 10, 78, 12, 15);
// Add the image data to the PDF document
doc.addImage(imgData, 'PNG', 10, 10, 190, 0);
$.ajax({
url: 'upload.php',
method: 'POST',
data: {
pdfData: doc.output('datauristring') // Convert the PDF to a data URI string
},
success: function(response) {
console.log('PDF saved successfully!');
},
error: function(xhr, status, error) {
console.error('Error saving k PDF:', error);
}
});
})
</script>
on server side code to accept AJAX reqjest with data - and save it on server location -
upload.php
<?php
// Get the PDF data from the POST request
$pdfData = $_POST['pdfData'];
file_put_contents('ciss.pdf', base64_decode(explode(',', $pdfData)[1]));
echo 'PDF saved successfully!';
?>
//------------- finish -----------
========== 2. Using PHP package - wkhtmltopdf =================
To install the wkhtmltopdf
library on your server.
Connect to your server: Use SSH or any other preferred method to log in to your server. run following commands -
sudo apt update
sudo apt-get update
sudo apt-get install -y libxrender1 libxext6 libfontconfig1 libjpeg-turbo8 libx11-6 fontconfig
sudo dpkg -i wkhtmltox_0.12.6-1.bionic_amd64.deb
sudo apt install wkhtmltopdf
wkhtmltopdf --version
create a php file on node server or any server and code like following
<?php
$url = 'https://flowerbet5.com/aaa.php'; // url from which you want to get your content
$savePath = 'aaaaaa.pdf'; // specify path like uploads/demo.pdf
exec("wkhtmltopdf $url $savePath");
if (file_exists($savePath)) {
echo 'Webpage converted and saved as PDF:
} else {
echo 'Error converting webpage to PDF.';
}
?>
Comments
Post a Comment