-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #479 from AOEpeople/update/#264409-replace-vue3-ht…
…ml2pdf Update/#264409 replace vue3 html2pdf
- Loading branch information
Showing
15 changed files
with
292 additions
and
552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<template> | ||
<div | ||
ref="content" | ||
:hidden="contentHidden" | ||
> | ||
<slot /> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref } from 'vue'; | ||
import useFlashMessage from '@/services/useFlashMessage'; | ||
import { FlashMessageType } from '@/enums/FlashMessage'; | ||
import { useI18n } from 'vue-i18n'; | ||
import useHtml2Pdf from '@/services/useHtml2Pdf'; | ||
const content = ref<HTMLDivElement | null>(null); | ||
const { t } = useI18n(); | ||
const { generatePdf } = useHtml2Pdf(); | ||
const props = withDefaults( | ||
defineProps<{ | ||
filename: string; | ||
contentHidden?: boolean; | ||
}>(), | ||
{ | ||
contentHidden: false | ||
} | ||
); | ||
async function downloadPdf() { | ||
if (content.value !== null) { | ||
content.value.hidden = false; | ||
await generatePdf(content.value, props.filename); | ||
content.value.hidden = props.contentHidden; | ||
} else { | ||
useFlashMessage().sendFlashMessage({ | ||
type: FlashMessageType.ERROR, | ||
message: t('flashmessage.print.error') | ||
}); | ||
} | ||
} | ||
defineExpose({ downloadPdf }); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { jsPDF } from 'jspdf'; | ||
import domtoimage from 'dom-to-image'; | ||
import useFlashMessage from '@/services/useFlashMessage'; | ||
import { FlashMessageType } from '@/enums/FlashMessage'; | ||
import { useI18n } from 'vue-i18n'; | ||
|
||
export default function useHtml2Pdf() { | ||
const { t } = useI18n(); | ||
|
||
async function generatePdf(content: HTMLDivElement, filename: string = 'result') { | ||
try { | ||
const imageUrl = await domtoimage.toPng(content, {}); | ||
|
||
const img = new Image(); | ||
img.src = imageUrl; | ||
img.onload = () => { | ||
const imgWidth = img.width; | ||
const imgHeight = img.height; | ||
const a4Ratio = 0.7070757462; // Ratio of width/height for an A4-Paper | ||
|
||
const pageWidth = imgWidth; | ||
const pageHeight = imgWidth / a4Ratio; | ||
const pdf = new jsPDF('p', 'px', [pageWidth, pageHeight]); | ||
|
||
for (let y = 0; y < imgHeight; y += pageHeight) { | ||
pdf.addImage(imageUrl, 'PNG', 0, -y, pageWidth, imgHeight); | ||
if (y + pageHeight < imgHeight) { | ||
pdf.addPage([pageWidth, pageHeight], 'p'); | ||
} | ||
} | ||
pdf.save(`${filename}.pdf`); | ||
}; | ||
} catch (error) { | ||
console.error('An error occured during pdf generation!', error); | ||
useFlashMessage().sendFlashMessage({ | ||
type: FlashMessageType.ERROR, | ||
message: t('flashmessage.print.error') | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
generatePdf | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.