import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
const htmlToPdf = {
getPdf (title) {
var shareContent = document.querySelector('#pdfDom')
var width = shareContent.offsetWidth / 4
var height = shareContent.offsetHeight / 4
html2Canvas(document.querySelector('#pdfDom'), {
allowTaint: true
}).then(canvas => {
var context = canvas.getContext('2d')
context.mozImageSmoothingEnabled = false
context.webkitImageSmoothingEnabled = false
context.msImageSmoothingEnabled = false
context.imageSmoothingEnabled = false
var pageData = canvas.toDataURL('image/jpeg', 1.0)
var img = new Image()
img.src = pageData
img.onload = function () {
// 获取dom高度、宽度
img.width = img.width / 2
img.height = img.height / 2
img.style.transform = 'scale(0.5)'
if (width > height) {
// 此可以根据打印的大小进行自动调节
// eslint-disable-next-line
var pdf = new JsPDF('l', 'mm', [
width * 0.505,
height * 0.545
])
} else {
// eslint-disable-next-line
var pdf = new JsPDF('p', 'mm', [
width * 0.505,
height * 0.545
])
}
pdf.addImage(
pageData,
'jpeg',
0,
0,
width * 0.505,
height * 0.545
)
pdf.save(title + '.pdf')
}
})
}
}
export default htmlToPdf