we have a rendered PDF!
TODOS: * actually apply styles to PDF * better error handling on client side
This commit is contained in:
@@ -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>
|
||||
|
||||
|
@@ -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' } });
|
||||
}
|
||||
|
Reference in New Issue
Block a user