Skip to content

Commit

Permalink
fix(pdf): await create context
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Sep 30, 2021
1 parent b30f9a8 commit d74bbd4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/components/editor/pdf/PDFPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/>
</svg>
</HeroIcon>
<div ref="preview" class="w-1/2 h-1/2 p-2"></div>
<div ref="preview" class="w-1/2 h-1/2"></div>
</template>

<script setup lang="ts">
Expand Down
6 changes: 3 additions & 3 deletions src/use/defines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const useDefines: Callback<any> = () => {
newProject: ['CTRL + Shift + Q', 'ctrl > shift > q'],
newChapter: ['CTRL + Q', 'ctrl > q'],
deleteChapter: ['CTRL + D', 'ctrl > d'],
configurationPDF: ['CTRL + Shift + G', 'ctrl > shift > g'],
previewPDF: ['CTRL + G', 'ctrl > g'],
generatePDF: ['CTRL + Shift + G', 'ctrl > shift > g'],
configurationPDF: ['CTRL + G', 'ctrl > g'],
previewPDF: ['CTRL + Shift + G', 'ctrl > shift > g'],
generatePDF: ['CTRL + Alt + G', 'ctrl > alt > g'],
switcherRawText: ['CTRL + H', 'ctrl > h'],
logger: ['CTRL + L', 'ctrl > l'],
}[k]
Expand Down
108 changes: 51 additions & 57 deletions src/use/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,58 +267,56 @@ export const usePDF: Callback<any> = () => {
return { content, styles, base }
}

const create: Callback<any> = (store: Store<any>): Promise<void> => {
return new Promise((resolve) => {
resolve(
pdfMake
.createPdf({
pageSize: generate().base(store).pageSize,
pageOrientation: generate().base(store).pageOrientation,
pageMargins: {
left: generate().base(store).pageMargins[0],
top: generate().base(store).pageMargins[1],
right: generate().base(store).pageMargins[2],
bottom: generate().base(store).pageMargins[3],
},
info: {
title: store.state.project.name,
author: 'TODO',
subject: store.state.project.version,
keywords: '',
},
content: generate().content(store),
styles: {
'heading-three': generate().styles(store).headingThree(),
'heading-two': generate().styles(store).headingTwo(),
'heading-one': generate().styles(store).headingOne(),
paragraph: generate().styles(store).paragraph(),
},
pageBreakBefore: function (
currentNode: any,
followingNodesOnPage: any,
nodesOnNextPage: any,
previousNodesOnPage: any
) {
//check if signature part is completely on the last page, add pagebreak if not
if (
currentNode.id === 'signature' &&
(currentNode.pageNumbers.length != 1 ||
currentNode.pageNumbers[0] != currentNode.pages)
) {
return true
}
//check if last paragraph is entirely on a single page, add pagebreak if not
else if (
currentNode.id === 'closingParagraph' &&
currentNode.pageNumbers.length != 1
) {
return true
}
return false
},
})
.download(`${store.state.project.nameRaw}.pdf`)
)
const create: Callback<any> = (store: Store<any>): void => {
const pdf = pdfMake.createPdf({
pageSize: generate().base(store).pageSize,
pageOrientation: generate().base(store).pageOrientation,
pageMargins: {
left: generate().base(store).pageMargins[0],
top: generate().base(store).pageMargins[1],
right: generate().base(store).pageMargins[2],
bottom: generate().base(store).pageMargins[3],
},
info: {
title: store.state.project.name,
author: 'TODO',
subject: store.state.project.version,
keywords: '',
},
content: generate().content(store),
styles: {
'heading-three': generate().styles(store).headingThree(),
'heading-two': generate().styles(store).headingTwo(),
'heading-one': generate().styles(store).headingOne(),
paragraph: generate().styles(store).paragraph(),
},
pageBreakBefore: function (
currentNode: any,
followingNodesOnPage: any,
nodesOnNextPage: any,
previousNodesOnPage: any
) {
//check if signature part is completely on the last page, add pagebreak if not
if (
currentNode.id === 'signature' &&
(currentNode.pageNumbers.length != 1 ||
currentNode.pageNumbers[0] != currentNode.pages)
) {
return true
}
//check if last paragraph is entirely on a single page, add pagebreak if not
else if (
currentNode.id === 'closingParagraph' &&
currentNode.pageNumbers.length != 1
) {
return true
}
return false
},
})

pdf.download(`${store.state.project.nameRaw}.pdf`, () => {
store.commit('absolute/load', false)
})
}

Expand Down Expand Up @@ -397,11 +395,7 @@ export const usePDF: Callback<any> = () => {

store.commit('absolute/load', true)

usePDF()
.create(store)
.then(() => {
store.commit('absolute/load', false)
})
usePDF().create(store)
}

const onPreviewPDF = async (input: HTMLElement) => {
Expand Down

0 comments on commit d74bbd4

Please sign in to comment.