From ba9522a8833f4b3b647b2e9408560c9cdc683fd6 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Mon, 4 Sep 2023 11:31:33 -0400 Subject: [PATCH] Generate PDF with os/exec process instead of js the htmltopdf.js library renders all PDF text as images instead of vectors, and Go does not have a reliable native library for generating PDFs that is actively maintained. The best solution appears to be to make a system call to wkhtmltopdf, but now there is a hidden package dependency that is not checked at build time and exists in another process. Maybe in the future a C library can be used and linked with the Go binary. --- components/dropdown.vue | 1 + components/estimate-test.vue | 60 ++++++++++++++++++++-- components/settings.vue | 3 +- skouter.go | 96 +++++++++++++++++++++++++++++++++++- views/pdf.tpl | 14 ++++++ views/test.tpl | 67 +++++++++++++++++++++++-- 6 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 views/pdf.tpl diff --git a/components/dropdown.vue b/components/dropdown.vue index cfb6085..1dff597 100644 --- a/components/dropdown.vue +++ b/components/dropdown.vue @@ -17,6 +17,7 @@ margin-top: 5px; z-index: 3; border: 1px solid black; + top: 100%; } .entry { diff --git a/components/estimate-test.vue b/components/estimate-test.vue index ce1d0b4..e0117f9 100644 --- a/components/estimate-test.vue +++ b/components/estimate-test.vue @@ -1,5 +1,6 @@ @@ -30,6 +35,7 @@ const doc = ref(null) const props = defineProps(['token', 'estimate', 'user']) const estimate = ref(null) const estimates = ref(null) +const pdfLink = ref('') const letterhead = computed(() => { if (!props.user.letterhead) return null @@ -43,7 +49,11 @@ const avatar = computed(() => { }) function makePDF() { - html2pdf(doc.value) + var opt = { + image: { type: 'png', quality: 1 }, + } + + html2pdf(doc.value, opt) } @@ -65,18 +75,62 @@ function getEstimates() { }) } +function getPdf() { + fetch(`/api/pdf`, + {method: 'GET', + headers: { + "Accept": "application/json", + "Authorization": `Bearer ${props.token}`, + }, + }).then(response => { + if (response.ok) { return response.blob() } + else { + return null + } + }).then (result => { + if (!result) return + pdfLink.value = URL.createObjectURL(result) + }) +} + onMounted(() => { getEstimates().then(() => estimate.value = estimates.value[0]) }) diff --git a/components/settings.vue b/components/settings.vue index fc48171..b966e7f 100644 --- a/components/settings.vue +++ b/components/settings.vue @@ -31,7 +31,7 @@ - +