Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate PDF

Shulou Source: shulou.com Published: 2022-06-03 03:45:23 09月27日 Update

This article will give you a detailed explanation on how to generate PDF. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Generate PDF at the backend

Thinkphp uses the MPDF plug-in

Sample code:

Public function pdf () {

/ / introduce the class library

Vendor ('mpdf.mpdf')

/ / set Chinese coding

$mpdf=new\ mPDF ('zh-cn','A4', 0,' Song style', 0,0)

/ / html content

$html=' a PDF file'

$mpdf- > WriteHTML ($html)

$mpdf- > Output ()

Exit

}

two。 Use the command line to link web pages to generate PDF

Download wkhtmltox

Tool command link generated PDF file

. / wkhtmltox/bin/wkhtmltopdf http://www.baidu.com test.pdf

3. Using js to generate PDF

Introduce the following two js plug-ins

Html2canvas.js

Jspdf.debug.js

Sample code:

Function explode () {

$(".ncap-order-details") .css ("background-color",'# FFFFFF')

Html2canvas ($(".ncap-order-details"), {

Onrendered:function (canvas) {

Var pageData = canvas.toDataURL (1. 0)

Var pdf = new jsPDF ('', 'pt', 'a4')

Pdf.addImage (pageData, 'JPEG', 0, 0, 595.28, 592.28/canvas.width * canvas.height)

Pdf.save ('stone.pdf')

}

})

}

Principle: first take screenshots, then insert screenshots into PDF

$(".ncap-order-details") is the div area to be screenshot. The screenshot defaults to a transparent background, so set the background color in advance, and then save the screenshot to PDF.

This is the end of the article on "how to generate PDF". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Generate screenshot article code content commands plug-ins files more examples background links good practical two regions principles background Arial tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Shulou Technology Redmi Linux