Skip to content

Commit

Permalink
feat(pdf): pdf compilation baybee
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 10, 2022
1 parent b70abd6 commit f3cf107
Show file tree
Hide file tree
Showing 61 changed files with 10,584 additions and 6,903 deletions.
3,433 changes: 3,433 additions & 0 deletions apps/converter-frontend-e2e/src/fixtures/indexnoimg.jats copy.xml

Large diffs are not rendered by default.

2,700 changes: 2,700 additions & 0 deletions apps/converter-frontend-e2e/src/fixtures/indexwithimg.jats.xml

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions apps/converter-frontend/pages/api/tex-to-pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { NextApiRequest, NextApiResponse } from 'next'
import tar from 'tar-stream'
import { WritableStreamBuffer } from 'stream-buffers'
import { streamToBuffer } from '@jorgeferrero/stream-to-buffer'
import { Writable } from 'stream'
import axios from 'axios'
import FormData from 'form-data'
import { fsyncSync } from 'fs'
import { writeFile } from 'fs/promises'

export default async (req: NextApiRequest, res: NextApiResponse) => {
req.statusCode = 200
const tex = req.body
const tarStream = new WritableStreamBuffer()
console.log('Received tex')

const tararr: any[] = []
const writable = new Writable({
write: (chunk, encoding, next) => {
console.log(chunk)
tararr.push(chunk)
next()
},
})
const formdata = new FormData()
const pack = tar.pack()
console.log('Packing tarball')
pack.entry({ name: 'article.tex' }, tex)
pack.on('finish', function () {})
console.log('Laying pipe')

pack.finalize()
pack.pipe(writable)
console.log('Converting tarball to buffer')
let tarball
writable.on('finish', async () => {
tarball = Buffer.concat(tararr)

formdata.append('files[]', tarball, 'tarball.tar.gz')
console.log('Posting tarball...')

axios
.post(
'https://texlive2020.latexonline.cc/data?command=lualatex&target=article.tex&force=true&download=sample.pdf',

formdata,
{
responseType: 'arraybuffer',
headers: {
...formdata.getHeaders(),
Accept: 'application/pdf',
},
}
)
.then(async (result) => {
console.log('PDF Received!')
console.log('Sending back result...')
const data = Buffer.from(result.data)
res.status(200)
res.setHeader('Content-Type', 'application/pdf')
res.send(data)
})
.catch(async (e) => {
console.log('Oopsie whoopsie, we made a fucky wucky!')
console.error(e)
res.status(400)
res.send(e)
})
})
}
38 changes: 35 additions & 3 deletions apps/converter-frontend/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,45 @@ export default function Index() {
)}
</Dropzone>
<Title>Input</Title>
<Code>
<pre>{thing}</pre>
<Code
style={{
maxHeight: '400px',
overflow: 'scroll',
}}
>
<pre
style={{
maxHeight: '400px',
overflow: 'scroll',
}}
>
{thing}
</pre>
</Code>
<Title>Output</Title>
<Button
onClick={() =>
fetch('/api/tex-to-pdf', { method: 'POST', body: tex })
.then((res) => res.blob())
.then((res) => {
window.open(URL.createObjectURL(res))
})
.catch((e) => console.error(e))
}
>
Try make pdf
</Button>
<Code>
<pre>{tex}</pre>
<pre
style={{
maxHeight: '400px',
overflow: 'scroll',
}}
>
{tex}
</pre>
</Code>

{/* <Container>
{[
'docx',
Expand Down
Binary file added article.pdf
Binary file not shown.
Loading

0 comments on commit f3cf107

Please sign in to comment.