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

add option to use local print server with headless puppeteer #1186

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions src/modules/map/components/openlayers/utils/usePrint.composable.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,45 @@ export function usePrint(map) {

const hostname = computed(() => store.state.ui.hostname)

async function printPup(server) {
try {
store.dispatch('setLoadingBarRequester', { requester, ...dispatcher })
if (currentJobReference.value) {
await abortCurrentJob()
}
printStatus.value = PrintStatus.PRINTING
const searchParams = new URLSearchParams(location.hash.split('?')[1])
const sr = searchParams.get('sr') || '2056'
if (sr === '2056') {
searchParams.set('novector', true)
}
searchParams.delete('sr')
const resolution = store.state.print.selectedScale / 25.4 / 96
const w_3857 = 40075016.68557849
const z = Math.log(w_3857 / 256 / resolution) / Math.log(2) + 1
searchParams.set('z', z)
searchParams.set('px', store.getters.selectedDPI / 96)
searchParams.set('scale', store.state.print.selectedScale)
const layoutParts = store.state.print.selectedLayout.name.split(' ')
searchParams.set('pdf', `${layoutParts[1]}_${layoutParts[2][0].toUpperCase()}`)
const printUrl = `${server}?${searchParams.toString()}`
console.log(printUrl)
currentJobReference.value = '111'
const result = await fetch(printUrl)
printStatus.value = PrintStatus.FINISHED_SUCCESSFULLY
return URL.createObjectURL(await result.blob())
} catch (error) {
log.error('Error while printing', error)
if (printStatus.value === PrintStatus.PRINTING) {
printStatus.value = PrintStatus.FINISHED_FAILED
}
return null
} finally {
store.dispatch('clearLoadingBarRequester', { requester, ...dispatcher })
currentJobReference.value = null
}
}

/**
* @param {Boolean} printGrid Print the coordinate grid on the finished PDF, true or false
* @param {Boolean} printLegend Print all visible layer legend (if they have one) on the map,
Expand Down Expand Up @@ -116,6 +155,7 @@ export function usePrint(map) {

return {
print,
printPup,
abortCurrentJob,
printStatus,
printError,
Expand Down
22 changes: 18 additions & 4 deletions src/modules/menu/components/print/MenuPrintSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const sectionId = 'printSection'
const isSectionShown = ref(false)
const printGrid = ref(false)
const printLegend = ref(false)
const printServer = ref('http://localhost:5555/puppeteer/print_sync')

const olMap = inject('olMap')
const { printStatus, print, abortCurrentJob, printError } = usePrint(olMap)
const { printStatus, print, printPup, abortCurrentJob, printError } = usePrint(olMap)

const i18n = useI18n()
const store = useStore()
Expand Down Expand Up @@ -99,12 +100,21 @@ function close() {

async function printMap() {
try {
const documentUrl = await print(printGrid.value, printLegend.value)
let documentUrl = ''
if (printServer.value !== '') {
documentUrl = await printPup(printServer.value)
} else {
documentUrl = await print(printGrid.value, printLegend.value)
}
if (documentUrl) {
if (window.navigator.userAgent.indexOf('MSIE ') > -1) {
window.open(documentUrl)
} else {
window.location = documentUrl
const link = document.createElement('a')
link.href = documentUrl
link.download = 'map.pdf'
link.click()
// window.location = documentUrl
}
} else {
if (printStatus.value === PrintStatus.FINISHED_ABORTED) {
Expand Down Expand Up @@ -158,7 +168,7 @@ defineExpose({
@click="selectLayout(layout)"
>
<!-- on the backend the layout are enumerated to keep the ordering, but here we don't want the
enumeration therefore we remove it -->
enumeration therefore we remove it -->
{{ layout.name.replace(/^\d+\.\s*/, '') }}
</option>
</select>
Expand Down Expand Up @@ -195,6 +205,10 @@ defineExpose({
/>
<label class="form-check-label" for="checkboxGrid">{{ i18n.t('graticule') }}</label>
</div>
<label for="printServer" class="col-form-label fw-bold me-2">{{
i18n.t('print_srv_url')
}}</label>
<input id="printServer" v-model="printServer" class="form-control" />
<div class="full-width">
<input
hidden
Expand Down
Loading