Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple HTML to PDF logic from saving functionality for improved flexibility #17

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
<div style="position: fixed">
<button
style="border-radius: 0; font-size: 20px; cursor: pointer"
@click="exportToPDF('pdf_export.pdf', pdfSection, undefined, {html2canvas: {scale: 0.7, useCORS: true}})"
@click="exportToPDF('pdf_export.pdf', pdfSection as HTMLElement, undefined, {html2canvas: {scale: 0.7, useCORS: true}})"
>
Generate normal PDF
</button>
<br>
<button
style="border-radius: 0; font-size: 20px; cursor: pointer; margin-top: 10px"
@click="printProtected(pdfSection)"
@click="printProtected(pdfSection as HTMLElement)"
>
Generate protected PDF
</button>
<br>
<button
style="border-radius: 0; font-size: 20px; cursor: pointer; margin-top: 10px"
@click="openInWindow(pdfSection as HTMLElement)"
>
Generate custom PDF and open in window
</button>
</div>
<div ref="pdfSection" style="width: 500px; margin: auto">
<TestingStage />
Expand All @@ -23,11 +30,11 @@

<script setup lang="ts">
import { ref } from 'vue'
import { exportToPDF } from '#imports'
import { exportToPDF, htmlToPdf } from '#imports'

const pdfSection = ref<HTMLElement | null>(null)

const printProtected = (HTMLElement: HTMLElement | undefined) => {
const printProtected = (HTMLElement: HTMLElement) => {
exportToPDF('pdf_protected_export.pdf', HTMLElement,
{
encryption: {
Expand All @@ -42,6 +49,25 @@ const printProtected = (HTMLElement: HTMLElement | undefined) => {
}
})
}

const openInWindow = async (HTMLElement: HTMLElement) => {
const pdf = await htmlToPdf(HTMLElement,
undefined,
{
html2canvas: {
scale: 0.7,
useCORS: true
}
})
const totalPages = pdf.getNumberOfPages()
const pdfHeight = pdf.getPageHeight()
await pdf.html('<b>I am a custom pdf!!!</b>', {
x: 20,
y: (pdfHeight - 50) * totalPages // place in the bottom
})
const blob = pdf.output('blob')
window.open(URL.createObjectURL(blob), '_blank')
}
</script>

<style>
Expand Down
22 changes: 5 additions & 17 deletions src/runtime/composables/exportToPDF.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import jsPDF, { HTMLOptions, jsPDFOptions } from 'jspdf'
import { HTMLOptions, jsPDFOptions } from 'jspdf'
import { htmlToPdf } from './htmlToPdf'

export const exportToPDF = async (
fileName: string,
element?: HTMLElement,
element: HTMLElement,
documentOptions?: jsPDFOptions,
options?: HTMLOptions
htmlOptions?: HTMLOptions
) => {
if (!(element instanceof HTMLElement)) {
throw new TypeError('usePDFExport: element is not a HTMLElement.')
}
const orientation = (element.offsetWidth > element.offsetHeight) ? 'l' : 'p'

// eslint-disable-next-line new-cap
const pdf = new jsPDF({
orientation: documentOptions?.orientation ?? orientation,
unit: documentOptions?.unit ?? 'px',
format: documentOptions?.format ?? 'A4',
encryption: documentOptions?.encryption
})

await pdf.html(element, options)
const pdf = await htmlToPdf(element, documentOptions, htmlOptions)
return pdf.save(fileName)
}
23 changes: 23 additions & 0 deletions src/runtime/composables/htmlToPdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import jsPDF, { HTMLOptions, jsPDFOptions } from 'jspdf'

export const htmlToPdf = async (
element: HTMLElement,
documentOptions?: jsPDFOptions,
htmlOptions?: HTMLOptions
) => {
if (!(element instanceof HTMLElement)) {
throw new TypeError('usePDFExport: element is not a HTMLElement.')
}
const orientation = (element.offsetWidth > element.offsetHeight) ? 'l' : 'p'

// eslint-disable-next-line new-cap
const pdf = new jsPDF({
orientation: documentOptions?.orientation ?? orientation,
unit: documentOptions?.unit ?? 'px',
format: documentOptions?.format ?? 'A4',
encryption: documentOptions?.encryption
})

await pdf.html(element, htmlOptions)
return pdf
}