tex.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import logging
  2. import tempfile
  3. import os.path
  4. import subprocess
  5. from . import lily_pb2, lily_pb2_grpc
  6. def _tmp_root():
  7. return os.path.join(tempfile.tempdir())
  8. class Service(lily_pb2_grpc.TexServicer):
  9. def ToPdf(self, request, context):
  10. logging.info("convert tex to pdf(%d)" % len(request.files))
  11. with tempfile.TemporaryDirectory(prefix='tex-') as root:
  12. for name in request.files:
  13. with open(os.path.join(root, name), mode='wb') as fd:
  14. logging.debug("generate file %s/%s", root, name)
  15. fd.write(request.files[name])
  16. for x in range(2):
  17. subprocess.run(
  18. ['xelatex', '-halt-on-error', 'main.tex'], cwd=root)
  19. with open(os.path.join(root, 'main.pdf'), mode="rb") as fd:
  20. response = lily_pb2.File()
  21. response.content_type = 'application/pdf'
  22. response.payload = fd.read()
  23. return response
  24. def ToWord(self, request, context):
  25. logging.info("convert tex to word %s" % request.content_type)
  26. response = lily_pb2.File()
  27. return response