This commit is contained in:
aihe 2024-06-05 14:09:00 +08:00
parent c0a18a0411
commit cd028eefab
2 changed files with 61 additions and 1 deletions

View File

@ -15,7 +15,9 @@
"license": "MIT",
"dependencies": {
"dayjs": "^1.11.10",
"file-saver": "2.0.5"
"file-saver": "2.0.5",
"pdf-lib": "1.17.1"
},
"dcloudext": {
"category": [

View File

@ -236,6 +236,64 @@
}
}
</script>
<script module="acceptance" lang="renderjs">
import { PDFDocument, StandardFonts } from "pdf-lib/dist/pdf-lib.esm.min"
export default {
mounted() {
// const script = document.createElement('script')
// // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
// script.src = 'static/js/jquery-3.1.1.min.js'
// script.onload = this.initEvent.bind(this, this.$ownerInstance)
// document.head.appendChild(script)
this.initEvent()
},
methods: {
async initEvent(ownerInstance) {
// 通过事件委托给按钮添加事件
// 添加事件前先清除事件避免重复绑定
// $(document).off("touchend")
// 新增
// $(document).on('touchend', '.add', function() {
// const wbsId = this.getAttribute('wbsId');
// const taskId = this.getAttribute('taskId');
// const taskName = this.getAttribute('taskName');
// // 调用 service 层的方法
// ownerInstance.callMethod('toAdd', {
// wbsId: wbsId,
// taskId: taskId,
// taskName: taskName
// })
// });
// Create a new PDFDocument
const pdfDoc = await PDFDocument.create()
// Embed the Times Roman font
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
// Add a page and draw some text on it
const page = pdfDoc.addPage([500, 600])
page.setFont(timesRomanFont)
page.drawText('The Life of an Egg', { x: 60, y: 500, size: 50 })
page.drawText('An Epic Tale of Woe', { x: 125, y: 460, size: 25 })
// Set all available metadata fields on the PDFDocument. Note that these fields
// are visible in the "Document Properties" section of most PDF readers.
pdfDoc.setTitle('🥚 The Life of an Egg 🍳')
pdfDoc.setAuthor('Humpty Dumpty')
pdfDoc.setSubject('📘 An Epic Tale of Woe 📖')
pdfDoc.setKeywords(['eggs', 'wall', 'fall', 'king', 'horses', 'men'])
pdfDoc.setProducer('PDF App 9000 🤖')
pdfDoc.setCreator('pdf-lib (https://github.com/Hopding/pdf-lib)')
pdfDoc.setCreationDate(new Date('2018-06-24T01:58:37.228Z'))
pdfDoc.setModificationDate(new Date('2019-12-21T07:00:11.000Z'))
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()
console.log("=========pdfBytes============", pdfBytes)
},
}
}
</script>
<style lang="scss">
@import '@/common/uni-nvue.scss';