Skip to content

Instantly share code, notes, and snippets.

@ap--
Last active December 19, 2015 13:29
Show Gist options
  • Select an option

  • Save ap--/5962224 to your computer and use it in GitHub Desktop.

Select an option

Save ap--/5962224 to your computer and use it in GitHub Desktop.
python command line tool for compiling tex documents to pdf online.
#!/usr/bin/env python
import requests
def compile2pdf(ifile, ofile):
"""
Sends the tex file to sciencesoft.at and compiles it to pdf.
requires two file objects.
"""
URL = 'http://sciencesoft.at/image/latexurl/%s' % ofile.name
OPTIONS = {'src' : unicode(ifile.read(), 'utf-8'),
'dev' : 'pdfwrite',
'papersize' : 'a4',
'dpi' : 600,
'result' : 'false',
'template' : 'no'}
r = requests.post(URL, params=OPTIONS)
r.raise_for_status()
with ofile as f:
f.write(r.content)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(prog='onlinetex',
description='Complile latex file to pdf using http://sciencesoft.at')
parser.add_argument('infile', type=argparse.FileType('r'))
parser.add_argument('--out', '-o', nargs='?', type=argparse.FileType('w'))
args = parser.parse_args()
IF = args.infile
OF = args.out if args.out else open(IF.name.replace('.tex','.pdf'),'w')
# ~~~ Magic ~~~ #
compile2pdf(IF, OF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment