#!/usr/bin/env python3

import os
import sys
import socket
import urllib.request
import subprocess
import tempfile

print("Content-Type: application/pdf\n")


resultat = subprocess.run(
	["python3", "fetch.py", "1"],
	capture_output=True,  # Capture ce que le script affiche avec print()
	text=True,            # Convertit automatiquement les octets en chaîne de caractères (str)
	check=True
)
code_latex = resultat.stdout

compiler = subprocess.run(
	["luatex", "-interaction=nonstopmode", "-output-format=pdf", "-output-directory=texoutput", "-jobname=fiche", "\\input /dev/stdin"],
	input=code_latex.encode('utf-8'),
	capture_output=True,
	text=False
)

with open("texoutput/fiche.pdf", "rb") as f:
	pdf_data = f.read()
sys.stdout.buffer.write(pdf_data)
