we have a rendered PDF!

TODOS:

* actually apply styles to PDF
* better error handling on client side
This commit is contained in:
2024-11-23 23:49:24 -06:00
parent 369f7e8946
commit a90b3c888e
6 changed files with 60 additions and 12 deletions

View File

@@ -18,13 +18,18 @@
async function performRender(event: MouseEvent) {
event.preventDefault();
console.log(
await fetch('/render', {
method: 'POST',
body: await output,
headers: { 'content-type': 'text/html' },
}),
);
const renderResponse = await fetch('/render', {
method: 'POST',
body: await output,
headers: { 'content-type': 'text/html' },
});
if (!renderResponse.ok) {
return;
}
const docURL = URL.createObjectURL(await renderResponse.blob());
window.open(docURL, '_blank');
}
</script>

View File

@@ -1,4 +1,6 @@
import { error, json, type RequestEvent } from '@sveltejs/kit';
import child_process from 'node:child_process';
import { error, type RequestEvent } from '@sveltejs/kit';
export async function POST({ request }: RequestEvent) {
if (!(request.headers.get('Content-Type') ?? 'bad').includes('text/html')) {
@@ -10,7 +12,21 @@ export async function POST({ request }: RequestEvent) {
}
const input = await request.text();
console.log('received input: ', input);
return json({ msg: 'to be implemented' });
const {
status: exitCode,
stderr: err,
stdout: output,
} = child_process.spawnSync('pandoc', ['--sandbox', '-f', 'html', '-t', 'pdf'], {
input,
timeout: 5000,
uid: 9999,
});
if (exitCode !== 0) {
console.error('pandoc errored: ', err.toString());
error(500, 'pandoc returned error response in server');
}
return new Response(output, { headers: { 'content-type': 'application/pdf' } });
}