def generatePDF(html_source, verbose, language_code='en', pdf_parameter='openpdf'): file_name = 'MuseScore-' + language_code + '.pdf' print 'Create PDF handbook:',file_name try: import ho.pisa as pisa if verbose: pisa.showLogging() except: print "\nPisa library required from creating PDFs. See README.txt for information\n" return #import re #html_source = re.sub('(.)','\\1 ',html_source) #m = re.search(">([^<]*)<",h) #m.group(0) #if (language_code == 'ja'): # html_source = insertSpaces(html_source) pdf = pisa.CreatePDF( html_source, file(file_name, "wb")) if not pdf.err and pdf_parameter=='openpdf': pisa.startViewer(file_name)
def helloWorld(): filename = __file__ + ".pdf" datauri = pisa.makeDataURIFromFile('img/denker.png') bguri = os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir, "pdf/background-sample.pdf")) bguri = pisa.makeDataURIFromFile(bguri) html = """ <style> @page { background: url("%s"); @frame text { top: 6cm; left: 4cm; right: 4cm; bottom: 4cm; -pdf-frame-border: 1; } } </style> <p> Hello <strong>World</strong> <p> <img src="%s"> """ % (bguri, datauri) pdf = pisa.pisaDocument( html, open(filename, "wb"), path = __file__ ) if not pdf.err: pisa.startViewer(filename)
def testURL( url="http://www.htmltopdf.org", dest="test-website.pdf"): """ Loading from an URL. We open a file like object for the URL by using 'urllib'. If there have to be loaded more data from the web, the pisaLinkLoader helper is passed as 'link_callback'. The pisaLinkLoader creates temporary files for everything it loads, because the Reportlab Toolkit needs real filenames for images and stuff. Then we also pass the url as 'path' for relative path calculations. """ import urllib pdf = pisa.CreatePDF( urllib.urlopen(url), file(dest, "wb"), log_warn = 1, log_err = 1, path = url, link_callback = pisa.pisaLinkLoader(url).getFileName ) dumpErrors(pdf) if not pdf.err: pisa.startViewer(dest)
def helloWorld(): filename = __file__ + ".pdf" pdf = pisa.CreatePDF("Hello <strong>World</strong>", file(filename, "wb")) if not pdf.err: pisa.startViewer(filename) if __name__ == "__main__": pisa.showLogging()
def helloWorld(): filename = __file__ + ".pdf" datauri = pisa.makeDataURIFromFile('img/denker.png') bguri = os.path.normpath( os.path.join(os.path.abspath(__file__), os.pardir, "pdf/background-sample.pdf")) bguri = pisa.makeDataURIFromFile(bguri) html = """ <style> @page { background: url("%s"); @frame text { top: 6cm; left: 4cm; right: 4cm; bottom: 4cm; -pdf-frame-border: 1; } } </style> <p> Hello <strong>World</strong> <p> <img src="%s"> """ % (bguri, datauri) pdf = pisa.pisaDocument(html, open(filename, "wb"), path=__file__) if not pdf.err: pisa.startViewer(filename)
def render_to_pdf(template, filename='out.pdf'): data = dict(small=range(10), large=range(200)) html = str(jinja_env.get_template(template).render(data)) with open(filename, 'wb') as doc: pdf = pisa.CreatePDF(html, doc) if not pdf.err: pisa.startViewer(filename)
def helloWorld(): filename = __file__ + ".pdf" pdf = pisa.CreatePDF( u"Hello <strong>World</strong>", file(filename, "wb") ) if not pdf.err: pisa.startViewer(filename)
def helloWorld(): filename = __file__ + ".pdf" pdf = pisa.CreatePDF( "Hello <strong>World</strong> <img src='data:image/jpg;base64,?�*'>", open(filename, "wb"), show_error_as_pdf=True, ) if not pdf.err: pisa.startViewer(filename)
def helloWorld(): filename = __file__ + ".pdf" pdf = pisa.CreatePDF( u"Hello <strong>World</strong> <img src='data:image/jpg;base64,?�*'>", file(filename, "wb"), show_error_as_pdf=True, ) if not pdf.err: pisa.startViewer(filename)
def exportToPdf(response, records): template = Template(filename=settings.TEMPLATE_DIRS[0] + '/html_table_export.html') template.output_encoding = 'utf-8' html_text = template.render(params={'records': records}) pdf = xhtml2pdf.CreatePDF(html_text, response) if not pdf.err: xhtml2pdf.startViewer(response) return response
def printPdf(text,openPdf,filename): text = wrap(text, 100) print "Writing to PDF file: " + filename f = file(filename, "wb") pdf = pisa.CreatePDF(text, f) if openPdf: if not pdf.err: pisa.startViewer(filename) f.close() print "Writing to PDF file DONE!"
def test(filename): # Convert HTML to "Reportlab Story" structure story = pisa.pisaStory(""" <h1>Sample</h1> <p>Hello <b>World</b>!</p> """ * 20).story # Draw to Canvas c = Canvas(filename) f = Frame(inch, inch, 6 * inch, 9 * inch, showBoundary=1) f.addFromList(story, c) c.save() # Show PDF pisa.startViewer(filename)
def test(filename): # Convert HTML to "Reportlab Story" structure story = pisa.pisaStory(""" <h1>Sample</h1> <p>Hello <b>World</b>!</p> """ * 20).story # Draw to Canvas c = Canvas(filename) f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1) f.addFromList(story,c) c.save() # Show PDF pisa.startViewer(filename)
def HTML2PDF(data, filename, open=False): """ Simple test showing how to create a PDF file from PML Source String. Also shows errors and tries to start the resulting PDF """ pdf = pisa.CreatePDF( cStringIO.StringIO(data), file(filename, "wb")) if open and (not pdf.err): pisa.startViewer(filename) return not pdf.err
def helloWorld(): filename = __file__ + ".pdf" lc = myLinkLoader(database="some_name", port=666).getFileName pdf = pisa.CreatePDF( u""" <p> Hello <strong>World</strong> <p> <img src="apath/some.png"> """, file(filename, "wb"), link_callback = lc, ) if not pdf.err: pisa.startViewer(filename)
def report_regiao(request, regiao='NE'): if request.POST: if 'regiao' in request.POST: regiao = request.POST['regiao'] REGIAO_CHOICES = dict(UnidadeFederativa.REGIAO_CHOICES) projetos = Projeto.objects.all() camaras = CasaLegislativa.objects.filter(tipo__sigla='CM') tabelas = list() # Geral convenios = Convenio.objects.filter(casa_legislativa__tipo__sigla='CM') tabela = casas_estado_to_tabela(camaras, convenios, regiao) tabela["projeto"] = _(u"Geral") tabelas.append(tabela) for projeto in projetos: convenios_proj = convenios.filter(projeto=projeto) tabela = casas_estado_to_tabela(camaras, convenios_proj, regiao) tabela["projeto"] = projeto.nome tabelas.append(tabela) data = datetime.datetime.now().strftime('%d/%m/%Y') hora = datetime.datetime.now().strftime('%H:%M') pisa.showLogging() response = HttpResponse(content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename=RelatorioRegiao_' + regiao + '.pdf' #tabelas = ({'projeto':"PI"},{'projeto':"PML"},) t = loader.get_template('convenios/tabela_regiao.html') c = Context({ 'tabelas': tabelas, 'regiao': REGIAO_CHOICES[regiao], 'data': data, 'hora': hora }) pdf = pisa.CreatePDF(t.render(c), response) if not pdf.err: pisa.startViewer(response) return response
def report_regiao(request,regiao='NE'): if request.POST: if request.POST.has_key('regiao'): regiao = request.POST['regiao'] REGIAO_CHOICES = { 'SL': 'Sul', 'SD': 'Sudeste', 'CO': 'Centro-Oeste', 'NE': 'Nordeste', 'NO': 'Norte', } projetos = Projeto.objects.all() camaras = CasaLegislativa.objects.filter(tipo__sigla='CM') tabelas = list() # Geral convenios = Convenio.objects.filter(casa_legislativa__tipo__sigla='CM') tabela = casas_estado_to_tabela(camaras,convenios,regiao) tabela["projeto"] = "Geral" tabelas.append(tabela) for projeto in projetos: convenios_proj = convenios.filter(projeto=projeto) tabela = casas_estado_to_tabela(camaras, convenios_proj,regiao) tabela["projeto"] = projeto.nome tabelas.append(tabela) data = datetime.datetime.now().strftime('%d/%m/%Y') hora = datetime.datetime.now().strftime('%H:%M') pisa.showLogging() response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=RelatorioRegiao_' + regiao + '.pdf' #tabelas = ({'projeto':"PI"},{'projeto':"PML"},) t = loader.get_template('convenios/tabela_regiao.html') c = Context({'tabelas':tabelas,'regiao':REGIAO_CHOICES[regiao],'data':data,'hora':hora}) pdf = pisa.CreatePDF(t.render(c),response) if not pdf.err: pisa.startViewer(response) return response
def testSimple( data="""Hello <b>World</b><br/><img src="img/test.jpg"/>""", dest="test.pdf"): """ Simple test showing how to create a PDF file from PML Source String. Also shows errors and tries to start the resulting PDF """ pdf = pisa.CreatePDF( cStringIO.StringIO(data), file(dest, "wb") ) if pdf.err: dumpErrors(pdf) else: pisa.startViewer(dest)
def testBackgroundAndImage( src="test-background.html", dest="test-background.pdf"): """ Simple test showing how to create a PDF file from PML Source String. Also shows errors and tries to start the resulting PDF """ pdf = pisa.CreatePDF( file(src, "r"), file(dest, "wb"), log_warn = 1, log_err = 1, path = os.path.join(os.getcwd(), src) ) dumpErrors(pdf) if not pdf.err: pisa.startViewer(dest)
def create(data, filename): f = file(filename, 'wb') pdf = pisa.CreatePDF(data, f) f.close() if not pdf.err: pisa.startViewer(filename)