Browse Source

feat: 中文字体乱码问题

master
taqi be 1 month ago
parent
commit
99edec47e5
  1. 98
      src/pages/deviceInfo/testPDF.vue
  2. 8
      src/utils/YaHei-normal.ts

98
src/pages/deviceInfo/testPDF.vue

@ -228,6 +228,97 @@
<script setup lang="ts"> <script setup lang="ts">
import html2canvas from 'html2canvas' import html2canvas from 'html2canvas'
import jsPDF from 'jspdf' import jsPDF from 'jspdf'
import '@/utils/YaHei-normal'
// async function generateAndPrintPDF() {
// const printArea = document.getElementById('printArea')
// if (!printArea) {
// console.error('')
// return
// }
// try {
// // HTML Canvas
// const canvas = await html2canvas(printArea, {
// scale: window.devicePixelRatio * 2,
// })
// // Canvas
// const canvasWidth = canvas.width
// const canvasHeight = canvas.height
// // Canvas
// const imgData = canvas.toDataURL('image/jpeg', 1.0)
// // jsPDF
// const pdf = new jsPDF('p', 'mm', 'a4')
// const pdfWidth = pdf.internal.pageSize.getWidth() // PDF
// const pdfHeight = pdf.internal.pageSize.getHeight() // PDF
// const headerHeight = 20 // Header
// const footerHeight = 20 // Footer
// const contentHeight = pdfHeight - headerHeight - footerHeight //
// // PDF
// const imgHeight = (canvasHeight * pdfWidth) / canvasWidth
// // PDF
// if (imgHeight <= contentHeight) {
// pdf.text('Header Title', pdfWidth / 2, headerHeight / 2, { align: 'center' }) // Header
// pdf.addImage(imgData, 'JPEG', 0, headerHeight, pdfWidth, imgHeight)
// pdf.text('Footer Text', pdfWidth / 2, pdfHeight - footerHeight / 2, {
// align: 'center',
// }) // Footer
// } else {
// //
// let position = 0 //
// while (position < canvasHeight) {
// const pageCanvas = document.createElement('canvas')
// pageCanvas.width = canvasWidth
// pageCanvas.height = (contentHeight * canvasWidth) / pdfWidth
// const pageCtx = pageCanvas.getContext('2d')
// pageCtx?.drawImage(
// canvas,
// 0,
// position, // Canvas
// canvasWidth,
// pageCanvas.height,
// 0,
// 0,
// canvasWidth,
// pageCanvas.height
// )
// const pageImgData = pageCanvas.toDataURL('image/jpeg', 1.0)
// // Header
// pdf.text('Header Title', pdfWidth / 2, headerHeight / 2, { align: 'center' })
// // PDF
// pdf.addImage(pageImgData, 'JPEG', 0, headerHeight, pdfWidth, contentHeight)
// // Footer
// pdf.text('Footer Text', pdfWidth / 2, pdfHeight - footerHeight / 2, {
// align: 'center',
// })
// position += pageCanvas.height //
// //
// if (position < canvasHeight) {
// pdf.addPage()
// }
// }
// }
// // PDF
// pdf.autoPrint()
// window.open(pdf.output('bloburl'), '_blank')
// } catch (error) {
// console.error(' PDF :', error)
// }
// }
async function generateAndPrintPDF() { async function generateAndPrintPDF() {
const printArea = document.getElementById('printArea') const printArea = document.getElementById('printArea')
@ -250,6 +341,7 @@ async function generateAndPrintPDF() {
const pdfWidth = pdf.internal.pageSize.getWidth() const pdfWidth = pdf.internal.pageSize.getWidth()
const pdfHeight = pdf.internal.pageSize.getHeight() const pdfHeight = pdf.internal.pageSize.getHeight()
pdf.setFontSize(12) pdf.setFontSize(12)
pdf.setFont('YaHei')
const headerHeight = 20 // Header const headerHeight = 20 // Header
const footerHeight = 20 // Footer const footerHeight = 20 // Footer
@ -262,7 +354,7 @@ async function generateAndPrintPDF() {
const imgHeight = (canvasHeight * contentWidth) / canvasWidth const imgHeight = (canvasHeight * contentWidth) / canvasWidth
const PDFInsertTextX = pdfWidth / 2 const PDFInsertTextX = pdfWidth / 2
const PDFInsertTextY = pdfHeight / 2 const PDFInsertTextY = headerHeight / 2
const PDFInsertFooterTextY = pdfHeight - footerHeight / 2 const PDFInsertFooterTextY = pdfHeight - footerHeight / 2
// PDF // PDF
@ -292,6 +384,10 @@ async function generateAndPrintPDF() {
const pageImgData = pageCanvas.toDataURL('image/jpeg', 1.0) const pageImgData = pageCanvas.toDataURL('image/jpeg', 1.0)
pdf.text('北京比塔技术服务有限公司', PDFInsertTextX, PDFInsertTextY, {
align: 'center',
})
// PDF // PDF
pdf.addImage( pdf.addImage(
pageImgData, pageImgData,

8
src/utils/YaHei-normal.ts

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save