MapComplete/src/UI/DownloadFlow/DownloadPdf.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.7 KiB
Svelte
Raw Normal View History

2023-06-04 23:58:29 +02:00
<script lang="ts">
import DownloadButton from "./DownloadButton.svelte"
import ThemeViewState from "../../Models/ThemeViewState"
import { SvgToPdf } from "../../Utils/svgToPdf"
import type { PdfTemplateInfo } from "../../Utils/svgToPdf"
import Translations from "../i18n/Translations"
import { Translation } from "../i18n/Translation"
import { Utils } from "../../Utils"
import { AvailableRasterLayers } from "../../Models/RasterLayers"
import Constants from "../../Models/Constants"
import Locale from "../i18n/Locale"
import { UIEventSource } from "../../Logic/UIEventSource"
import DownloadHelper from "./DownloadHelper"
2023-11-23 15:47:16 +01:00
import Qr from "../../Utils/Qr"
2023-06-04 23:58:29 +02:00
export let templateName: string
export let state: ThemeViewState
const template: PdfTemplateInfo = SvgToPdf.templates[templateName]
let t = Translations.t.general.download
const downloadHelper = new DownloadHelper(state)
2023-06-04 23:58:29 +02:00
async function constructPdf(_, title: string, status: UIEventSource<string>) {
title =
title.substring(0, title.length - 4) + "_" + template.format + "_" + template.orientation
const templateUrls = SvgToPdf.templates[templateName].pages
const templates: string[] = await Promise.all(templateUrls.map((url) => Utils.download(url)))
console.log("Templates are", templates)
const bg = state.mapProperties.rasterLayer.data ?? AvailableRasterLayers.maptilerDefaultLayer
const creator = new SvgToPdf(title, templates, {
state,
freeComponentId: "belowmap",
2023-11-15 03:42:37 +01:00
createImage: (key: string, width: string, height: string) => {
console.log("Creating an image for key", key)
2023-11-23 15:47:16 +01:00
if (key === "qr") {
2023-11-15 03:42:37 +01:00
const toShare = window.location.href.split("#")[0]
return new Qr(toShare).toImageElement(parseFloat(width), parseFloat(height))
}
2023-11-23 15:47:16 +01:00
return downloadHelper.createImage(key, width, height)
2023-11-15 03:42:37 +01:00
},
textSubstitutions: <Record<string, string>>{
"layout.title": state.layout.title,
layoutid: state.layout.id,
title: state.layout.title,
layoutImg: state.layout.icon,
version: Constants.vNumber,
date: new Date().toISOString().substring(0, 16),
background: new Translation(bg.properties.name).txt,
},
})
2023-06-04 23:58:29 +02:00
const unsub = creator.status.addCallbackAndRunD((s) => {
status?.setData(s)
})
await creator.ExportPdf(Locale.language.data)
unsub()
return undefined
}
2023-06-04 23:58:29 +02:00
</script>
<DownloadButton
construct={constructPdf}
extension="pdf"
helperText={t.downloadAsPdfHelper}
metaIsIncluded={false}
2023-11-23 15:47:16 +01:00
mainText={t.pdf.current_view_generic.Subs({
orientation: template.orientation,
paper_size: template.format.toUpperCase(),
})}
mimetype="application/pdf"
{state}
2023-06-04 23:58:29 +02:00
/>