Skip to content

Commit

Permalink
Merge pull request #479 from AOEpeople/update/#264409-replace-vue3-ht…
Browse files Browse the repository at this point in the history
…ml2pdf

Update/#264409 replace vue3 html2pdf
  • Loading branch information
MalibusParty authored Jul 2, 2024
2 parents bc1551b + 54eb9d8 commit 2713ee5
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 552 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tests/e2e/cypress/screenshots/
test_ci_cypress.sh
tests/e2e/cypress/e2e/FillDBWithStuff.cy.ts
tests/e2e/cypress/downloads/result.pdf
tests/e2e/cypress/downloads/teilnehmerliste.pdf
src/Resources/style/output.css.map

###> phpunit/phpunit ###
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"axios": "^0.28.0",
"core-js": "^3.8.3",
"datatables.net": "^1.11",
"dom-to-image": "^2.6.0",
"dotenv-webpack": "^7.1.0",
"easy-autocomplete": "1.3.5",
"epic-spinners": "^2.0.0",
"jspdf": "^2.5.1",
"moment": "^2.29.4",
"npm": "^8.19.2",
"postcss-cli": "^9.1.0",
Expand All @@ -45,7 +47,6 @@
"vue-router": "4",
"vue-screen": "^2.1.0-beta.1",
"vue-template-compiler": "^2.6.14",
"vue3-html2pdf": "^1.1.2",
"vue3-tabs": "^0.1.13-beta.3",
"webpack-merge": "^5.8.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/src/components/finance/FinancePdfTemplate.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<template>
<div
v-if="finances !== null && finances.transactions !== undefined"
class="w-[700px]"
class="w-full p-8"
>
<h1 class="mb-4 text-start">
{{ finances.heading }}
</h1>
<FinanceTable
:transactions="finances.transactions"
:print="true"
class="w-full"
/>
</div>
</template>
Expand Down
45 changes: 45 additions & 0 deletions src/Resources/src/components/pdfCreator/PdfCreator.vue
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>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<div
v-if="listData !== null && mealNames.length > 0 && participationCount.length > 0"
class="w-[700px]"
class="w-full px-8"
>
<div class="text-center">
<div class="w-full text-center">
<svg
class="mr-2 inline min-w-[36px] align-text-bottom text-primary"
width="36"
Expand Down Expand Up @@ -36,12 +36,12 @@
{{ t('printList.title') + dateString }}
</h1>
</div>
<div class="w-[700px]">
<div class="flex w-[700px] flex-col">
<div class="w-[700px]">
<div class="inline-block w-[700px] min-w-full py-2 align-middle">
<div class="aoe-shadow w-[700px] ring-1 ring-black/5 md:rounded-lg">
<table class="w-[700px] min-w-full divide-y divide-gray-300">
<div class="w-full">
<div class="flex w-full flex-col">
<div class="w-full">
<div class="inline-block w-full min-w-full py-2 align-middle">
<div class="aoe-shadow w-full ring-1 ring-black/5 md:rounded-lg">
<table class="w-full min-w-full divide-y divide-gray-300">
<thead class="bg-white">
<tr class="divide-y divide-gray-200">
<th
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
"guest": {
"joined": "Wir freuen uns Sie dabei zu haben."
}
},
"print": {
"error": "Beim Erstellen des PDF's ist ein Fehler aufgetreten."
}
},
"finance": {
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
"guest": {
"joined": "We are looking forward to meeting you."
}
},
"print": {
"error": "An error occured during the creation of the pdf."
}
},
"finance": {
Expand Down
45 changes: 45 additions & 0 deletions src/Resources/src/services/useHtml2Pdf.ts
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
};
}
46 changes: 14 additions & 32 deletions src/Resources/src/views/Costs.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
<template>
<Vue3Html2pdf
v-if="loaded === true"
ref="html2pdf"
:html-to-pdf-options="{
filename: 'kosten.pdf',
image: { type: 'jpeg', quality: 0.98 },
margin: 10,
jsPDF: { unit: 'mm' },
html2canvas: { dpi: 192, letterRendering: true }
}"
:pdf-quality="2"
pdf-format="a4"
pdf-orientation="portrait"
pdf-content-width="700px"
:manual-pagination="true"
>
<template #pdf-content>
<SimpleCostsTable ref="costsTableToPrint" />
</template>
</Vue3Html2pdf>
<div class="mx-[5%] xl:mx-auto">
<CostsHeader
v-model="filter"
:show-hidden="showHidden"
:print-active="loaded"
class="mb-4"
@change:show-hidden="(value) => (showHidden = value)"
@print-costs="() => generatePdf()"
@print-costs="pdfCreator?.downloadPdf()"
/>
<CostsTable
v-if="loaded === true"
Expand All @@ -36,6 +16,17 @@
/>
<LoadingSpinner :loaded="loaded" />
</div>
<PdfCreator
v-if="loaded === true"
ref="pdfCreator"
filename="kosten"
:content-hidden="true"
>
<SimpleCostsTable
ref="costsTableToPrint"
class="px-8 pt-8"
/>
</PdfCreator>
</template>

<script setup lang="ts">
Expand All @@ -45,15 +36,14 @@ import { useCosts } from '@/stores/costsStore';
import CostsTable from '@/components/costs/CostsTable.vue';
import CostsHeader from '@/components/costs/CostsHeader.vue';
import LoadingSpinner from '@/components/misc/LoadingSpinner.vue';
import Vue3Html2pdf from 'vue3-html2pdf';
import PdfCreator from '@/components/pdfCreator/PdfCreator.vue';
import SimpleCostsTable from '@/components/costs/SimpleCostsTable.vue';
const { fetchCosts } = useCosts();
const loaded = ref(false);
const filter = ref('');
const showHidden = ref(false);
const html2pdf = ref(null);
const costsTableToPrint = ref(null);
const pdfCreator = ref(null);
onMounted(async () => {
const progress = useProgress().start();
Expand All @@ -63,12 +53,4 @@ onMounted(async () => {
progress.finish();
});
function generatePdf() {
if (html2pdf.value) {
html2pdf.value.generatePdf();
} else {
console.log('Html2Pdf not found');
}
}
</script>
44 changes: 15 additions & 29 deletions src/Resources/src/views/Finance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,36 @@
:date-range="finance.heading"
:show-controls="Number(index) === 0"
@date-changed="handleDateChange"
@generate-pdf="download()"
@generate-pdf="pdfCreator?.downloadPdf()"
/>
<FinanceTable :transactions="finance.transactions" />
</div>
<div class="m-4 p-4">
<Vue3Html2pdf
ref="html2pdf"
:html-to-pdf-options="{ filename: 'result.pdf', image: { type: 'png' }, margin: 10, jsPDF: { unit: 'mm' } }"
:pdf-quality="2"
pdf-format="a4"
pdf-orientation="portrait"
pdf-content-width="700px"
:manual-pagination="true"
>
<template #pdf-content>
<FinancePdfTemplate
v-if="loaded && FinancesState.finances !== undefined && FinancesState.finances.length > 0"
:finances="FinancesState.finances[0]"
/>
</template>
</Vue3Html2pdf>
</div>
<PdfCreator
v-if="loaded === true"
ref="pdfCreator"
filename="finanzen"
:content-hidden="true"
>
<FinancePdfTemplate
v-if="loaded && FinancesState.finances !== undefined && FinancesState.finances.length > 0"
:finances="FinancesState.finances[0]"
class="mt-8"
/>
</PdfCreator>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import FinanceTable from '@/components/finance/FinanceTable.vue';
import { useFinances } from '@/stores/financesStore';
import FinanceHeader from '@/components/finance/FinanceHeader.vue';
import Vue3Html2pdf from 'vue3-html2pdf';
import FinancePdfTemplate from '@/components/finance/FinancePdfTemplate.vue';
import PdfCreator from '@/components/pdfCreator/PdfCreator.vue';
const { fetchFinances, FinancesState } = useFinances();
const loaded = ref(false);
const html2pdf = ref(null);
const pdfCreator = ref(null);
onMounted(async () => {
await fetchFinances();
Expand All @@ -53,12 +47,4 @@ onMounted(async () => {
const handleDateChange = (modelData: Date[]) => {
fetchFinances(modelData);
};
function download() {
if (html2pdf.value) {
html2pdf.value.generatePdf();
} else {
console.log('Html2Pdf not found');
}
}
</script>
Loading

0 comments on commit 2713ee5

Please sign in to comment.