OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Finding solution for converting html code to text-based PDF

  • Thread starter Thread starter Lý Công Thành
  • Start date Start date
L

Lý Công Thành

Guest
So basically i need to convert my html code to a pdf file using js or java

I have been trying out several tools and came across one called html2pdf, which works well for dynamically converting CSS and HTML files. However, the main issue is that html2pdf currently converts HTML to an image and then pushes it to a PDF file. This results in a PDF file that cannot be searched or copied. Since we can't change the fact that html2pdf turns HTML into an image, I am looking for a new tool that can convert HTML and CSS to PDF using Java or JavaScript.


Code:
window.onload = function () {
    document.getElementById("download").addEventListener("click", () => {
        const allvoice = document.getElementById("allvoice");

        var opt = {
            margin : [20,0],
            filename: 'myfile.pdf',
            image: { type: 'png', quality: 0.98 }, 
            html2canvas: {
                scale: 2,
                logging: true,
                dpi: 192,
                letterRendering: true
            },
            jsPDF: {
                unit: 'mm',
                format: 'a4',
                orientation: 'landscape',
            },
            pagebreak: {  mode: ['avoid-all', 'css', 'legacy'] }
        };

        html2pdf().from(allvoice).set(opt).toPdf().get('pdf').then(function(pdf) {
            var totalPages = pdf.internal.getNumberOfPages();
            for (i = 1; i <= totalPages; i++) {
                pdf.setPage(i);
                pdf.setFontSize(10);
                pdf.setTextColor(100);
                pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth() / 2.3), (pdf.internal.pageSize.getHeight() - 0.8));
            }
        }).save();
    });
}
<p>So basically i need to convert my html code to a pdf file using js or java</p>
<p>I have been trying out several tools and came across one called html2pdf, which works well for dynamically converting CSS and HTML files. However, the main issue is that html2pdf currently converts HTML to an image and then pushes it to a PDF file. This results in a PDF file that cannot be searched or copied. Since we can't change the fact that html2pdf turns HTML into an image, I am looking for a new tool that can convert HTML and CSS to PDF using Java or JavaScript.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>window.onload = function () {
document.getElementById("download").addEventListener("click", () => {
const allvoice = document.getElementById("allvoice");

var opt = {
margin : [20,0],
filename: 'myfile.pdf',
image: { type: 'png', quality: 0.98 },
html2canvas: {
scale: 2,
logging: true,
dpi: 192,
letterRendering: true
},
jsPDF: {
unit: 'mm',
format: 'a4',
orientation: 'landscape',
},
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
};

html2pdf().from(allvoice).set(opt).toPdf().get('pdf').then(function(pdf) {
var totalPages = pdf.internal.getNumberOfPages();
for (i = 1; i <= totalPages; i++) {
pdf.setPage(i);
pdf.setFontSize(10);
pdf.setTextColor(100);
pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth() / 2.3), (pdf.internal.pageSize.getHeight() - 0.8));
}
}).save();
});
}</code></pre>
</div>
</div>
</p>
Continue reading...
 

Latest posts

Top