Neville Ausgetauscht + PDF
This commit is contained in:
@@ -4,14 +4,14 @@ import { Checkbox, Flex, Text } from "./index.js"
|
||||
import { Layout } from "./Layout.js"
|
||||
import { PDFElement } from "./PDFElement.js"
|
||||
|
||||
export function xml2pdf(xml: string, font: PDFFont) {
|
||||
export function xml2pdf(xml: string, fonts: Record<string, PDFFont> & { "default": PDFFont }) {
|
||||
const tree = txml.parse(xml)
|
||||
|
||||
const iterateChildren = (children: ReturnType<typeof txml.parse>, parent: PDFElement) => {
|
||||
for (const child of children) {
|
||||
if (typeof child === "string") {
|
||||
// Simple text
|
||||
parent.addChild(new Text(child, { font }))
|
||||
parent.addChild(new Text(child, { font: fonts["default"] }))
|
||||
} else if (child.tagName === "flex") {
|
||||
const flexbox = new Flex({
|
||||
height: parseFloat(child.attributes.height) || "auto",
|
||||
@@ -19,7 +19,19 @@ export function xml2pdf(xml: string, font: PDFFont) {
|
||||
align: child.attributes.align,
|
||||
direction: child.attributes.direction,
|
||||
gap: parseFloat(child.attributes.gap) || 0,
|
||||
justify: child.attributes.justify
|
||||
justify: child.attributes.justify,
|
||||
margin: {
|
||||
bottom: parseFloat(child.attributes.marginBottom) || 0,
|
||||
left: parseFloat(child.attributes.marginLeft) || 0,
|
||||
right: parseFloat(child.attributes.marginRight) || 0,
|
||||
top: parseFloat(child.attributes.marginTop) || 0,
|
||||
},
|
||||
padding: {
|
||||
bottom: parseFloat(child.attributes.paddingBottom) || 0,
|
||||
left: parseFloat(child.attributes.paddingLeft) || 0,
|
||||
right: parseFloat(child.attributes.paddingRight) || 0,
|
||||
top: parseFloat(child.attributes.paddingTop) || 0,
|
||||
}
|
||||
})
|
||||
|
||||
iterateChildren(child.children, flexbox)
|
||||
@@ -37,8 +49,19 @@ export function xml2pdf(xml: string, font: PDFFont) {
|
||||
|
||||
color = rgb(...colorValue.map((x) => parseInt(x) / 255) as [number, number, number]);
|
||||
}
|
||||
|
||||
const text = new Text(child.children[0] || "", { font: child.attributes.hasOwnProperty("bold") ? bold : font, fontSize: parseFloat(child.attributes.size) || 10, color })
|
||||
|
||||
const text = new Text(child.children[0] || "", { font: child.attributes.hasOwnProperty("font") ? fonts[child.attributes["font"]] : fonts["default"], lineHeight: parseFloat(child.attributes.lineHeight), fontSize: parseFloat(child.attributes.size) || 10, color, margin: {
|
||||
bottom: parseFloat(child.attributes.marginBottom) || 0,
|
||||
left: parseFloat(child.attributes.marginLeft) || 0,
|
||||
right: parseFloat(child.attributes.marginRight) || 0,
|
||||
top: parseFloat(child.attributes.marginTop) || 0,
|
||||
},
|
||||
padding: {
|
||||
bottom: parseFloat(child.attributes.paddingBottom) || 0,
|
||||
left: parseFloat(child.attributes.paddingLeft) || 0,
|
||||
right: parseFloat(child.attributes.paddingRight) || 0,
|
||||
top: parseFloat(child.attributes.paddingTop) || 0,
|
||||
} })
|
||||
|
||||
parent.addChild(text)
|
||||
} else if (child.tagName === "checkbox") {
|
||||
@@ -77,29 +100,41 @@ export function xml2pdf(xml: string, font: PDFFont) {
|
||||
return layout
|
||||
}
|
||||
|
||||
const pdf = await PDFDocument.create()
|
||||
// const pdf = await PDFDocument.create()
|
||||
|
||||
const page = pdf.addPage()
|
||||
// const page = pdf.addPage()
|
||||
|
||||
const font = await pdf.embedFont(StandardFonts.Helvetica)
|
||||
const bold = await pdf.embedFont(StandardFonts.HelveticaBold)
|
||||
// const font = await pdf.embedFont(StandardFonts.Helvetica)
|
||||
// const bold = await pdf.embedFont(StandardFonts.HelveticaBold)
|
||||
|
||||
const layout = xml2pdf(`<layout height="${page.getHeight()}" width="${page.getWidth()}" marginTop="0" marginLeft="0">
|
||||
<text bold size="40">Hello</text>
|
||||
<text color="255,0,255" size="24">Hello, how are you?</text>
|
||||
<flex direction="row" gap="8" align="center">
|
||||
<checkbox height="100" width="100"></checkbox>
|
||||
<text>Hello</text>
|
||||
<layout marginLeft="0">
|
||||
<text>Hello</text>
|
||||
<text>Nasya, i love you</text>
|
||||
</layout>
|
||||
</flex>
|
||||
</layout>`, font)
|
||||
// console.log(page.getWidth(), "WIDTH");
|
||||
|
||||
layout.draw(page, 0, page.getHeight())
|
||||
|
||||
import { writeFileSync } from "fs"
|
||||
import { FixedLengthArray } from "#lib/Berechnungen/BedarfsausweisWohnen/types.js"
|
||||
// const layout = xml2pdf(`
|
||||
// <flex direction="column" justify="end" width="${page.getWidth()}" height="${page.getHeight()}">
|
||||
// <flex direction="row" gap="5" align="center">
|
||||
// <checkbox width="8" height="8"></checkbox>
|
||||
// <text size="12">awd1</text>
|
||||
// </flex>
|
||||
// <flex direction="row" gap="5" align="center">
|
||||
// <checkbox width="8" height="8"></checkbox>
|
||||
// <text size="12">awd2</text>
|
||||
// </flex>
|
||||
// <flex direction="row" gap="5" align="center">
|
||||
// <checkbox width="8" height="8"></checkbox>
|
||||
// <text size="12">awd3</text>
|
||||
// </flex>
|
||||
// <flex direction="row" gap="5" align="center">
|
||||
// <checkbox width="8" height="8"></checkbox>
|
||||
// <text size="12">awd4</text>
|
||||
// </flex>
|
||||
// </flex>`, {
|
||||
// "default": font
|
||||
// })
|
||||
|
||||
writeFileSync("./test-pdf.pdf", await pdf.save())
|
||||
// layout.draw(page, 0, page.getHeight())
|
||||
|
||||
// import { writeFileSync } from "fs"
|
||||
// import { FixedLengthArray } from "#lib/Berechnungen/BedarfsausweisWohnen/types.js"
|
||||
|
||||
// writeFileSync("./test-pdf.pdf", await pdf.save())
|
||||
Reference in New Issue
Block a user