-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9d6ba1
commit 61e3860
Showing
14 changed files
with
222 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { PDFFont, PDFPage } from 'pdf-lib' | ||
|
||
import { CardInfo } from '../../generated/card_pb' | ||
import { Region } from '../../generated/graphql' | ||
import CardBlueprint from '../CardBlueprint' | ||
import { Coordinates, PdfElement, PdfElementRenderer, mmToPt } from './PdfElements' | ||
|
||
export type InfoParams = { | ||
info: CardInfo | ||
region: Region | ||
cardBlueprint: CardBlueprint | ||
} | ||
|
||
type PdfDetailElementProps = { | ||
width: number | ||
fontSize: number | ||
infoToDetails: (info: InfoParams) => string | ||
} & Coordinates | ||
|
||
type PdfDetailElementRendererProps = { | ||
page: PDFPage | ||
font: PDFFont | ||
info: CardInfo | ||
region: Region | ||
cardBlueprint: CardBlueprint | ||
} | ||
|
||
export type PdfDetailElementRenderer = PdfElementRenderer<PdfDetailElementRendererProps> | ||
|
||
const PdfDetailElement: PdfElement<PdfDetailElementProps, PdfDetailElementRendererProps> = | ||
({ width, x, y, fontSize, infoToDetails }) => | ||
({ page, font, info, region, cardBlueprint }) => { | ||
const text = infoToDetails({ info, region, cardBlueprint }) | ||
|
||
const lineHeight = mmToPt(5) | ||
|
||
page.drawText(text, { | ||
font, | ||
x: mmToPt(x), | ||
y: page.getSize().height - mmToPt(y) - lineHeight, | ||
maxWidth: mmToPt(width), | ||
wordBreaks: text.split('').filter(c => !'\n\f\r\u000B'.includes(c)), // Split on every character | ||
lineHeight, | ||
size: fontSize, | ||
}) | ||
} | ||
|
||
export default PdfDetailElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type Coordinates = { | ||
x: number | ||
y: number | ||
} | ||
|
||
export function mmToPt(mm: number) { | ||
return (mm / 25.4) * 72 | ||
} | ||
|
||
export type PdfElementRenderer<T extends Record<string, any>> = (info: T) => void | ||
export type PdfElement<P extends Record<string, any>, T extends Record<string, any>> = ( | ||
options: P | ||
) => PdfElementRenderer<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { PDFPage } from 'pdf-lib' | ||
|
||
import { QrCode } from '../../generated/card_pb' | ||
import { drawQRCode } from '../../util/qrcode' | ||
import { Coordinates, PdfElement, PdfElementRenderer, mmToPt } from './PdfElements' | ||
|
||
type PdfQrCode = Extract<QrCode['qrCode'], { case: 'staticVerificationCode' | 'dynamicActivationCode' }> | ||
|
||
type PdfQrCodeElementProps = { | ||
size: number | ||
} & Coordinates | ||
|
||
type PdfQrCodeElementRendererProps = { | ||
page: PDFPage | ||
qrCode: PdfQrCode | ||
} | ||
|
||
export type PdfQrCodeElementRenderer = PdfElementRenderer<PdfQrCodeElementRendererProps> | ||
|
||
const PdfQrCodeElement: PdfElement<PdfQrCodeElementProps, PdfQrCodeElementRendererProps> = | ||
({ size, x, y }) => | ||
({ page, qrCode }) => { | ||
const qrCodeSizePdf = mmToPt(size) | ||
const qrCodeXPdf = mmToPt(x) | ||
const qrCodeYPdf = page.getSize().height - qrCodeSizePdf - mmToPt(y) | ||
|
||
const qrCodeContent = new QrCode({ | ||
qrCode: qrCode, | ||
}).toBinary() | ||
|
||
drawQRCode(qrCodeContent, qrCodeXPdf, qrCodeYPdf, qrCodeSizePdf, page, false) | ||
} | ||
|
||
export default PdfQrCodeElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { format } from 'date-fns' | ||
|
||
import PdfDetailElement, { InfoParams } from '../../cards/pdf/PdfDetailElement' | ||
import PdfQrCodeElement from '../../cards/pdf/PdfQrCodeElement' | ||
import { daysSinceEpochToDate } from '../../cards/validityPeriod' | ||
import { BavariaCardType } from '../../generated/card_pb' | ||
import { PdfConfig } from '../PdfConfig' | ||
// @ts-ignore | ||
import pdfTemplate from './pdf-template.pdf' | ||
|
||
const renderPdfInfo = ({ info, region }: InfoParams) => { | ||
const expirationDay = info.expirationDay ?? 0 | ||
const expirationDate = expirationDay > 0 ? format(daysSinceEpochToDate(expirationDay), 'dd.MM.yyyy') : 'unbegrenzt' | ||
|
||
const cardType = info.extensions?.extensionBavariaCardType?.cardType | ||
return `${info.fullName} | ||
Kartentyp: ${cardType === BavariaCardType.STANDARD ? 'Blau' : 'Gold'} | ||
Gültig bis: ${expirationDate} | ||
Ausgestellt am ${format(new Date(), 'dd.MM.yyyy')} | ||
von ${region.prefix} ${region.name}` | ||
} | ||
|
||
const pdfConfiguration: PdfConfig = { | ||
title: 'Ehrenamtskarten', | ||
templatePath: pdfTemplate, | ||
issuer: 'Bayerische Staatsministerium für Arbeit und Soziales, Familie und Integration', | ||
elements: { | ||
dynamicQrCodes: [PdfQrCodeElement({ x: 108, y: 73, size: 84 })], | ||
details: [PdfDetailElement({ x: 108, y: 170, width: 84, fontSize: 10, infoToDetails: renderPdfInfo })], | ||
}, | ||
} | ||
|
||
export default pdfConfiguration |
Oops, something went wrong.