def _generatePDF(template, context={}): """ Compute a pdf from a LaTeX @template, with some dynamic data in @context return a @GeneratedFile instance """ t = DjTemplate(template.content) c = DjContext(context) latex_str = t.render(c) latex_str += '' # to django.utils.safestring.SafeText to unicode latex_doc = LatexDocument(latex_str) try: gen_file = GeneratedFile() gen_file.template = template result = latex_doc.as_pdf(debug=True) # returns a StringIO instance except LatexDocument.LatexPdfGenerationError as e: gen_file.errors = "Error during pdf generation: %s" % str(e) logger.error(str(e)) else: gen_file.file.save("%s-%d" % (template.name, template.id,), ContentFile(result.read())) gen_file.save() return gen_file
def __init__(self, config_file="pretty.yml"): LatexDocument.__init__(self) self.__config = yaml.load(open(config_file, "r").read()) self.text_append_prefix = self.__config["LatexText"]["append_prefix"] self.text_append_suffix = self.__config["LatexText"]["append_suffix"] self.comment_prefix = self.__config["LatexComment"]["prefix"] self.comment_append_prefix = self.__config["LatexComment"]["append_prefix"] self.comment_append_suffix = self.__config["LatexComment"]["append_suffix"]
def test_getdocument(self): doc = LatexDocument() header = [LatexCommand("head"), LatexText("headtext")] content = [LatexCommand("content"), LatexText("contenttext")] doc.setHeader(header) doc.setContent(content) assert doc.getDocument() == r"""\head
def imprimir_relatorio(request): valor = _valor(request) veiculos = _consulta(valor) veiculos = list(veiculos) latex = render_to_string('veiculo/imprimir_relatorio.tex', {'veiculos': veiculos}) #Transdorma em documento latex tex = LatexDocument(latex.encode('utf-8')) #Transforma em pedf pdf = tex.as_pdf() response = HttpResponse(pdf, mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=%s.pdf' % 'relatorio' # abre o pdf na propria aba # return HttpResponse(response, mimetype='application/pdf') # baixar o arquivo direto return response
def save(req): json_obj = convert_json(req.POST) document_with_content = fill_format(json_obj, document) r = LatexDocument(document_with_content) pdf = r.as_pdf() return response(pdf, mimetype='pdf')
def test_set_content_line(self): doc = LatexDocument() doc.addContentLine(LatexCommand("test")) doc.addContentLine(LatexText("text")) assert doc.getLinesContent()[0].getString() == r"\test" and doc.getLinesContent()[1].getString() == r"text"
def test_set_header(self): doc = LatexDocument() doc.setHeader([LatexCommand("test"), LatexText("text")]) assert doc.getLinesHeader()[0].getString() == r"\test" and doc.getLinesHeader()[1].getString() == r"text"
def test_set_content(self): doc = LatexDocument() doc.setContent([LatexCommand("test"), LatexText("text")]) assert doc.getLinesContent()[0].getString() == r"\test" and doc.getLinesContent()[1].getString() == r"text"
def test_set_header_line(self): doc = LatexDocument() doc.addHeaderLine(LatexCommand("test")) doc.addHeaderLine(LatexText("text")) assert doc.getLinesHeader()[0].getString() == r"\test" and doc.getLinesHeader()[1].getString() == r"text"
#!/usr/bin/env python # coding: utf-8 from copy import deepcopy import math import sys from latex import LatexDocument, Math, Section, Text from fixed_precision import Num doc = LatexDocument() class Matrix(object): def __init__(self, matrix): self._matrix = matrix def __iter__(self): return iter(self._matrix) def __mul__(self, other): if not isinstance(other, type(self)): raise NotImplementedError( '{} * {}'.format(type(self).__name__, type(other).__name__) ) return Matrix([ [ sum(a * b for a, b in zip(self_row, other_col)) for other_col in zip(*other)
payer=[e.strip() for e in csvline.split(",")] if csvline.find("#obsolete")>0: continue fakturanr+=1 navn=payer[0] email=payer[1] adresse=payer[2] poststed=payer[3] fakturanrf="2012-%04d" % (fakturanr) (varelinje,total)=config["typer"][payer[4]] formattedprice=("% 8.2f" % total).replace(".",",") medlemsnr=payer[5] fakturaer.append(fakturanrf) with codecs.open("latex/faktura%s.tex" % fakturanrf, "w", encoding="UTF-8") as out: sum=0.0 ldoc=LatexDocument() ldoc.append(LE("documentclass","no.oao.girofaktura")) doc=ldoc.append(LatexBlock("document")) doc.append(LE("input","config.tex")) doc.append(LE("ToCompany","\\\\".join((navn,adresse,poststed)))) doc.append(LE("CustNo",medlemsnr)) doc.append(LE("YourRef","")) doc.append(LE("InvoiceNo",fakturanrf)) doc.append(LE("InvoiceDate",invoicedate)) doc.append(LE("LastDate",lastdate)) doc.append(LE("SumTot",total,"00")) doc.append(LE("InvoiceTop")) art=doc.append(LatexBlock("Articles")) art.append(LE("Article",varelinje,formattedprice)) art.append(LE("Divider")) art.append(LE("Sum"))