Skip to content
Open
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
7 changes: 4 additions & 3 deletions packages/server/client/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ function escapeText(s: string) {
return res;
}

export async function ConvertCodeToPDF(code: Buffer, lang, filename, team, location, codeColor = false) {
export async function ConvertCodeToPDF(code: Buffer, lang, filename, team, location, createAt, codeColor = false) {
compiler ||= await createTypstCompiler();
const fakeFilename = randomstring(8); // cubercsl: do not trust filename from user
const typst = generateTypst(team, location, fakeFilename, filename, lang, codeColor);
const typst = generateTypst(team, location, fakeFilename, filename, lang, createAt, codeColor);
compiler.addSource('/main.typst', typst);
compiler.addSource(`/${fakeFilename}`, escapeText(toUtf8(code)));
logger.info(`Convert ${filename} to PDF`);
Expand All @@ -82,14 +82,15 @@ export async function printFile(docs) {
const files = [];
for (const doc of docs) {
const {
_id, tid, code, lang, filename, team, location,
_id, tid, code, lang, filename, team, location, createAt,
} = doc;
const pdf = await ConvertCodeToPDF(
code ? Buffer.from(code, 'base64') : Buffer.from('empty file'),
lang,
filename,
team,
location,
createAt,
config.printColor,
);
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), pdf);
Expand Down
6 changes: 4 additions & 2 deletions packages/server/client/typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class TypstCompilerDriver {
}
}

export function generateTypst(team: string, location: string, filename: string, originalFilename: string, lang: string, codeColor: boolean) {
export function generateTypst(team: string, location: string, filename: string, originalFilename: string, lang: string, createAt: number, codeColor: boolean) {
return `
#let fit(name: "", width: 147mm) = {
#let fit(name: "", width: 100mm) = {
context {
if measure(text(name)).width < width {
return name
Expand Down Expand Up @@ -92,6 +92,8 @@ export function generateTypst(team: string, location: string, filename: string,
text(weight: "black", size: 10pt)[[#location] ]
}
#fit(name: team)
#h(1fr)
${new Date(createAt).toLocaleString('zh')}
#linebreak()
filename: #original
#h(1fr)
Expand Down
1 change: 1 addition & 0 deletions packages/server/handler/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PrintAdminHandler extends AuthHandler {
code.filename,
code.team,
code.location,
code.createAt,
params.color ?? true,
);
this.response.type = 'application/pdf';
Expand Down