Exemple #1
0
def issue(request, invoice_id):
    invoice = get_object_or_404(Invoice, pk=invoice_id)
    file_name = invoice.generate_filename()
    pdfkit.from_string(invoice.rendered_html, os.path.join(settings.MEDIA_ROOT, file_name))
    invoice.invoice_pdf_file = file_name
    invoice.save()
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def com_pdf_create(pdf_from, pdf_data, pdf_out_file):
    if pdf_from == 'url':
        pdfkit.from_url(pdf_data, pdf_out_file)
    elif pdf_from == 'file':
        pdfkit.from_file(pdf_data, pdf_out_file)
    else:
        pdfkit.from_string(pdf_data, pdf_out_file)
def fetch_and_save_evaluations(evaluation_row, webworker, instructors_uni):
    sep_char = '-'
    ext_char = '.pdf'
    instructor_eval = ''.join([sep_char, evaluation_row[4]]) if evaluation_row[4] != ' ' else ''
    filename = ''.join([evaluation_row[0], sep_char, evaluation_row[3], sep_char,
                        evaluation_row[2], instructor_eval, ext_char])
    fetch_url = evaluation_row[5]

    print('Getting evaluation: ' + filename)
    evaluation = webworker.get(fetch_url)
    if evaluation.status_code != 200:
        print('Error: Failed to fetch: ' + filename)
        return

    options = {
        'page-size': 'Letter',
        'margin-top': '0.25in',
        'margin-right': '0.25in',
        'margin-bottom': '0.25in',
        'margin-left': '0.25in',
        'encoding': "UTF-8",
        'quiet': ''
    }

    pdfkit.from_string(evaluation.text, instructors_uni + '/' + filename, options=options, css='eval.css')
def _format_email(name, address, email_address, crop, variety, before, day, sick_day, acre, sick_acre, comment, whole_view, single_view, feature_view, root_view):
    with open('templates/template.html', 'r') as f:
        the_str = f.read()
        the_template = Template(the_str.decode('utf-8'))

    the_map = {
        'name': name,
        'address': address,
        'email': email_address,
        'crop': crop,
        'variety': variety,
        'before': before,
        'day': day,
        'sick_day': sick_day,
        'acre': acre,
        'sick_acre': sick_acre,
        'comment': comment,
        'whole_view': whole_view,
        'single_view': single_view,
        'feature_view': feature_view,
        'root_view': root_view,
    }

    content = the_template.render(the_map).strip()

    filename = cfg.config.get('pdf_dir', '/data/agri_med/pdf') + '/' + util.gen_random_string() + '.pdf'

    pdfkit.from_string(content, filename)

    return content, filename
Exemple #5
0
def gen_pdf_f(words, file_name):
	
	html = gen_html(words)
	html = html.replace('margin-left: 350px;', 'margin-left: 20px;')

	out_pdf_patch = './' + data_folder + '/'+ file_name + '/' + file_name + '.pdf'
	pdfkit.from_string(html, out_pdf_patch)
def createPdfFromHtml(html, pdfFileName, quietOption = False):
    if quietOption == False:
        options = {}
    else:
        options = {'quiet': ''}

    pdfkit.from_string(html, fileName, options=options)
Exemple #7
0
def get_pdf(html, options=None, output=None):
    html = scrub_urls(html)
    html, options = prepare_options(html, options)
    fname = os.path.join("/tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash()))

    try:
        pdfkit.from_string(html, fname, options=options or {})
        if output:
            append_pdf(PdfFileReader(file(fname, "rb")), output)
        else:
            with open(fname, "rb") as fileobj:
                filedata = fileobj.read()

    except IOError, e:
        if (
            "ContentNotFoundError" in e.message
            or "ContentOperationNotPermittedError" in e.message
            or "UnknownContentError" in e.message
            or "RemoteHostClosedError" in e.message
        ):

            # allow pdfs with missing images if file got created
            if os.path.exists(fname):
                with open(fname, "rb") as fileobj:
                    filedata = fileobj.read()

            else:
                frappe.throw(_("PDF generation failed because of broken image links"))
        else:
            raise
Exemple #8
0
def create_attestation(request, registration_id, stagiaire_id):
    stagiaire = models.Stagiaire.objects.get(pk=stagiaire_id)
    registration = models.Registration.objects.get(pk=registration_id)
    context = {
        'stagiaire':stagiaire,
        'formation':registration.session.formation,
        'now': datetime.datetime.now().date(),
        'duree': models.Date.objects.filter(session=registration.session).count()*7
    }
    options = {
        'page-size': 'Letter',
        'margin-top': '2cm',
        'margin-right': '2cm',
        'margin-bottom': '2cm',
        'margin-left': '2cm',
        'encoding': "UTF-8",
        'no-outline': None
    }

    attestation_html_string = render_to_string('documents/attestation_basic.html', context)
    path_directory = 'media/registration_data/attestation/'+str(registration.key)
    if not os.path.exists(path_directory):
        os.makedirs(path_directory)

    attestation = models.AttestationFormation.objects.create(registration=registration,
                                                     stagiaire=stagiaire)
    #creating pdf
    pdfkit.from_string(attestation_html_string, 'media/registration_data/attestation/'+str(registration.key)+'/attestation_'+str(registration.key)+'_'+str(stagiaire.lastname)+'.pdf', options=options)
    attestation.attestation.name = 'registration_data/attestation/'+str(registration.key)+'/attestation_'+str(registration.key)+'_'+str(stagiaire.lastname)+'.pdf'
    attestation.save()
Exemple #9
0
def write_html_to_pdf(file_info,html):
	import time
	import pdfkit, os, frappe
	from frappe.utils import scrub_urls

	fname = 'HLSNP-{filename}.pdf'.format(filename=str(int(round(time.time() * 1000))))
	fpath = os.path.join(file_info.get('path'),fname)
	
	options = {}
	options.update({
		"print-media-type": None,
		"background": None,
		"images": None,
		'margin-top': '15mm',
		'margin-right': '15mm',
		'margin-bottom': '15mm',
		'margin-left': '15mm',
		'encoding': "UTF-8",
		'no-outline': None
	})
	html = scrub_urls(html)
	pdfkit.from_string(html, fpath, options=options or {})

	return {
		"path":fpath,
		"fname":fname
	}
Exemple #10
0
def pdf_export(html_string, file_name):
    '''
    Take a given html string and export the formatted output as
    a pdf file.

    Arguments:
      html_string -- a Python string contain
      file_name   -- a user submitted file name
    Returns:
      Success_value -- A tuple:(True, 'Success') / (False, 'reason for failure')
    '''
    # Strip .pdf from the file name if present
    if file_name.endswith('.pdf'):
        file_name = file_name[:-4]
    # Test file_name for invalid characters (using windows banned characters
    # to allow for cross platform as windows has the most restrictions).
    for char in file_name:
        if char in "\/:*?<>|":
            return (False, 'A file name can not contain any of the following characters: \\ / : * ? < > |')
    # Check for pre-existing files with the same name and warn the user if any are found.
    if os.path.exists(file_name):
        warning = QtGui.QMessageBox.question(
            "Warning!",
            "A file already exists with that name. Do you wish to replace it?",
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
            QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            pass
        else:
            return (False, 'Please select a unique file name.')
    pdfkit.from_string(html_string, '{}.pdf'.format(file_name))
    return (True, 'Success')
def save_article(html, thr_id):
    print('线程id: {}'.format(thr_id))
    soup = BeautifulSoup(html, 'lxml')
    if isinstance(soup, BeautifulSoup):
        # 获得文章内容
        content = soup.find('article')

        # 得到文章标题名称
        title = get_article_title(content)

        # 得到文章的分类信息
        category = get_article_category(content)

        # 创建存放文章的分类目录
        if not os.path.exists(os.path.join('archives', category)):
            os.mkdir(os.path.join('archives', category))

        # 实际保存后的文件名称
        name = '{}'.format(get_valid_name('{}'.format(title)))

        try:
            print('正在生成文件:{}.pdf,请稍等...'.format(title))
            article = get_pure_article(content)
            if article:
                # 生成PDF文件,这里的css文件是博客使用的样式
                pdfkit.from_string(article, os.path.join('archives', category, '{}.pdf'.format(name)),
                                   options=options, css='style.css')

            return True
        except Exception as e:
            print(str(e))
            return False

    return False
 def create_pdf_files(self):
     path = resource_filename('immunarray.lims', 'reports')
     reports = [r for r in os.listdir(path)
                if exists(join(path, r, 'report.pt'))]
     # generate report for all aliquots if ANY images exist for them.
     # the report is responsible for assuming correctly which images exist.
     for aliquot in self.get_all_aliquots():
         if not any([self.get_UnivarFigure(aliquot, 1),
                     self.get_UnivarFigure(aliquot, 2),
                     self.get_ReportFigure(aliquot)]):
             continue
         # available as `view.aliquot` in the report's template
         self.aliquot = aliquot
         for report in reports:
             # header and footer (if any) get rendered to actual html files
             options = {}
             htfn = join(path, report, 'report-header.pt')
             if exists(htfn):
                 hfn = tempfile.mktemp(suffix=".html")
                 html = ViewPageTemplateFile(htfn)(self).encode('utf-8')
                 open(hfn, 'w').write(html)
                 options['--header-html'] = hfn
             ftfn = join(path, report, 'report-footer.pt')
             if exists(ftfn):
                 ftn = tempfile.mktemp(suffix=".html")
                 html = ViewPageTemplateFile(ftfn)(self).encode('utf-8')
                 open(ftn, 'w').write(html)
                 options['--footer-html'] = ftn
             # then render the report template itself
             template = ViewPageTemplateFile(join(path, report, 'report.pt'))
             fn = join(self.outpath, '%s - %s' % (aliquot.title, report))
             html = unicode(template(self))
             pdfkit.from_string(html, fn + '.pdf', options=options)
Exemple #13
0
def save_list_to_file(token, filename=None, file_format='pdf', org_id=TIMEPAD_GRANUMSALIS_ORG_ID, date=None):

    if not date:
        date = datetime.datetime.now().strftime('%Y-%m-%d')

    if not filename:
        filename = TIMEPAD_LIST_FILENAME.format('-', date, file_format)

    info = get_timepad_info(token, org_id=org_id, date=date)

    if file_format == 'txt':
        with open(filename, 'w') as list_file:
            list_file.write(info.title.encode('utf-8'))
            for name in info.names_list:
                list_file.write(u'{0}\n'.format(name).encode('utf-8'))
    elif file_format == 'pdf':
        LIST_JADE = 'list.jade'
        LIST_HTML = 'list.html'
        OPTIONS_JSON = 'options.json'

        with open(OPTIONS_JSON, 'w') as options_file:
            json.dump(info, options_file)

        #jade_text = open('list.jade').read()
        #list_html = pyjade.simple_convert(jade_text)
        #open('list.html', 'w').write(list_html)

        # have to hack it through command exec since pyjade sucks
        os.system('jade -P -O {0} < {1} > {2}'.format(OPTIONS_JSON, LIST_JADE, LIST_HTML))
        list_html = codecs.open(LIST_HTML, encoding='utf-8').read()
        pdfkit.from_string(list_html, filename)
    else:
        raise "Format not supported"

    return filename
def Mypdf(request):
    # filename = 'create.pdf'
    # pdf = wkhtmltopdf(template)
    # return HttpResponse(pdf)





    # config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/html2pdf')
    # pdfkit.from_string('template', 'salary1.pdf', configuration=config)
    logger = logging.getLogger('pdf')
    logger.info('pdf file')
    config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/html2pdf')
    composers = ComposersProfile.objects.all()
    for composer in composers:
        context = Context({'my_name': composer.first_name,'last_name':composer.last_name })
        print context
        body = "<html>ggggggg</html>"
        template = Template("<table>My name is</table>{{ my_name }}{{ last_name }}")
        print template
        m = template.render(context)
        print m
        pdfkit.from_string(m, 'salary7.pdf', configuration=config)
        mail = EmailMultiAlternatives('Regarding Password Change','email_body','*****@*****.**',['*****@*****.**'])
        mail.attach_file('salary7.pdf')
        mail.send()
    logger.info('end')
    return HttpResponse('0')
Exemple #15
0
    def convert(self, content, view=None):
        item = IPDFHTMLProvider(content)
        html = item.pdf_html(view=view)
        try:
            css = os.path.dirname(os.path.abspath(__file__)) + '/browser/css/pdf.css'

            out = pdfkit.from_string(
                html,
                False,
                options=self._options(),
                configuration=self.config,
                css=css,
            )
        except IOError:
            # pt = getToolByName(content, 'portal_transforms')
            # texto = content.getText()
            # texto_puro = pt.convertTo('text/plain', texto, mimetype='text/html').getData().strip()
            html = '<!DOCTYPE html>' \
                   '<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br">' \
                   '    <head>' \
                   '        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' \
                   '    </head>' \
                   '    <body>' \
                   '        <h1>Erro ao gerar o PDF.</h1>' \
                   '        <p>Existe conteúdo externo na página.</p>' \
                   '    </body>' \
                   '</html>'
            out = pdfkit.from_string(html,
                False,
                options=self._options(),
                configuration=self.config
            )
        return StringIO(out)
Exemple #16
0
def get_pdf(html, options=None):
	if not options:
		options = {}

	options.update({
		"print-media-type": None,
		"background": None,
		"images": None,
		'margin-top': '15mm',
		'margin-right': '15mm',
		'margin-bottom': '15mm',
		'margin-left': '15mm',
		'encoding': "UTF-8",
		'quiet': None,
		'no-outline': None
	})

	if not options.get("page-size"):
		options['page-size'] = frappe.db.get_single_value("Print Settings", "pdf_page_size") or "A4"

	html = scrub_urls(html)
	fname = os.path.join("/tmp", frappe.generate_hash() + ".pdf")
	pdfkit.from_string(html, fname, options=options or {})

	with open(fname, "rb") as fileobj:
		filedata = fileobj.read()

	os.remove(fname)

	return filedata
Exemple #17
0
def build():
    parser = argparse.ArgumentParser(description='Generate PDF resume.')
    parser.add_argument('-o', dest='output_file', default='cv.pdf', help='Output file')
    args = parser.parse_args()

    html = render('./pdf/', 'template.html', content)
    pdfkit.from_string(html, args.output_file, options=dict(encoding="UTF-8"))
Exemple #18
0
def main():
    log = open(DATA_DIR + "log", "a")
    log.write(str(datetime.now()) + "-START-DAILY\n")

    log.write(str(datetime.now()) +  "-START-DAILY-EXTRACT\n")
    db_table = extract()
    log.write(str(datetime.now()) +  "-END-DAILY-EXTRACT\n")

    log.write(str(datetime.now()) +  "-START-DAILY-TRANSFORM\n")
    daily_report = transform(db_table)
    log.write(str(datetime.now()) +  "-END-DAILY-TRANSFORM\n")

    log.write(str(datetime.now()) +  "-START-DAILY-SAVEPDF\n")
    html_pdf = html(daily_report,2)
    PDFReport_Name = titles(2)
    pdfkit.from_string(html_pdf, DATA_DIR + PDFReport_Name)
    log.write(str(datetime.now()) +  "-END-DAILY-SAVEPDF\n")

    log.write(str(datetime.now()) +  "-START-DAILY-SENDMAIL\n")
    html_body = html(daily_report,1)
    Subject = titles(1)
    SendEmail(Subject,html_body,'*****@*****.**','*****@*****.**','123@datateam','',PDFReport_Name)
    SendEmail(Subject,html_body,'*****@*****.**','*****@*****.**','123@datateam','',PDFReport_Name)
    log.write(str(datetime.now()) +  "-END-DAILY-SENDMAIL\n")

    log.write(str(datetime.now()) + "-END-DAILY\n")
    log.close()
def generate_final_score(matches, team_count, round_number, scores, round_scores, adjustment, pdf_file):
    team_dict_ary = []
    for i in range(team_count):
        team_number = i + 1
        total_score = [ x[1] for x in scores if x[0] == team_number ][0]
        team_adjustment = [ x[1] for x in adjustment if x[0] == team_number ][0]
        adjusted_score = total_score + team_adjustment
        team_dict = {
            "team_number": team_number,
            "rounds": [],
            "score": total_score,
            "adjustment": team_adjustment,
            "adjusted_score": adjusted_score,
            "rank": -1
        }
        for j in range(round_number):
            print j
            print round_scores
            round_match = matches[j]            
            match = [ x for x in round_match if x[1] == team_number ][0]
            opp_team = match[2]
            table = int((match[0]+1)/2)
            current_round_score = round_scores[j]
            score = [ x[1] for x in current_round_score if x[0] == team_number ][0]
            rnd_score_entry = {
                "opp_team": opp_team,
                "table": table,
                "score": score
            }
            team_dict["rounds"].append(rnd_score_entry)
        team_dict_ary.append(team_dict)

    final_score_sorted_by_rank = sorted(team_dict_ary, reverse=True, key=itemgetter('adjusted_score'))
    i = 1;
    for team in final_score_sorted_by_rank:
        team["rank"] = i
        i = i + 1
    final_score_sorted_by_team = sorted(final_score_sorted_by_rank, key=itemgetter('team_number'))    

    f = open(FINAL_SCORE_TEMPLATE_PATH, "r")
    tmp_html = f.read()
    f.close()
    tmpl = Template(tmp_html)
    round_number_counts = [ x+1 for x in range(round_number)]
    html = tmpl.render({"teams":final_score_sorted_by_team, "round_numbers":round_number_counts })

    options = {
        'page-size': 'A4',
        'orientation': 'Landscape',
        'margin-top': '20mm',
        'margin-right': '20mm',
        'margin-bottom': '20mm',
        'margin-left': '20mm',
        'encoding': "UTF-8",
        'grayscale': None,
        'outline-depth':3,
        # 'footer-center':'[page]',
        'footer-line': None,
    }
    pdfkit.from_string(html, pdf_file, options=options)
Exemple #20
0
def get_pdf(html, options=None):
	if not options:
		options = {}

	options.update({
		"print-media-type": None,
		"background": None,
		"images": None,
		'margin-top': '15mm',
		'margin-right': '15mm',
		'margin-bottom': '15mm',
		'margin-left': '15mm',
		'encoding': "UTF-8",
		'quiet': None,
		'no-outline': None
	})

	if not options.get("page-size"):
		options['page-size'] = frappe.db.get_single_value("Print Settings", "pdf_page_size") or "A4"

	html = scrub_urls(html)
	fname = os.path.join("/tmp", frappe.generate_hash() + ".pdf")
	try:
		pdfkit.from_string(html, fname, options=options or {})
	except IOError, e:
		if "ContentNotFoundError" in e.message:
			frappe.throw(_("PDF generation failed because of broken image links"))
		else:
			raise
Exemple #21
0
 def generate_pdf_invoice(self, order):
     try:
         html_text = order_invoice_pdf_template.render(order=order)
         output_filename = os.path.join(
             invoice_emails_folder, "Invoice-%s.pdf" % (order['order_no']))
         pdfkit.from_string(html_text, output_filename)
     except Exception as e:
         self.log.exception(e)
Exemple #22
0
def main():
    log = open(DATA_DIR + "log_cohort", "a")
    log.write(str(datetime.now()) + "-START-COHORT\n")
    # EXTRACT ######################################################################## EXTRACT #
    log.write(str(datetime.now()) +  "-START-COHORT-EXTRACT\n")

    all_drivers_table = extract(1)
    first_driver_table = extract(2)
    all_pax_table = extract(3)
    first_pax_table = extract(4)
    all_drivers_table.to_csv(DATA_DIR + 'all_drivers_table', sep='\t')
    first_driver_table.to_csv(DATA_DIR + 'first_driver_table', sep='\t')
    all_pax_table.to_csv(DATA_DIR + 'all_pax_table', sep='\t')
    first_pax_table.to_csv(DATA_DIR + 'first_pax_table', sep='\t')

    log.write(str(datetime.now()) +  "-END-COHORT-EXTRACT\n")
    # TRANSFORM ######################################################################## TRANSFORM #
    log.write(str(datetime.now()) +  "-START-COHORT-TRANSFORM\n")

    driver_first_list = transform_1(first_driver_table)
    pax_first_list = transform_1(first_pax_table)

    cohort_driver = transform_2(first_driver_table, all_drivers_table, driver_first_list)
    cohort_passenger = transform_2(first_pax_table, all_pax_table, pax_first_list)

    cohort_size_driver = transform_3(driver_first_list)
    cohort_size_pax = transform_3(pax_first_list)

    cohort_driver_final = transform_4(cohort_driver)
    cohort_passenger_final = transform_4(cohort_passenger)

    log.write(str(datetime.now()) +  "-END-COHORT-TRANSFORM\n")
    # HTML ################################################################################## HTML #
    log.write(str(datetime.now()) +  "-START-COHORT-HTML\n")

    html_driver = html(cohort_driver_final,cohort_size_driver,1)
    html_pax = html(cohort_passenger_final,cohort_size_pax,2)

    log.write(str(datetime.now()) +  "-END-COHORT-HTML\n")
    # SAVE PDF ############################################################################ SAVE PDF #
    log.write(str(datetime.now()) +  "-START-COHORT-SAVEPDF\n")

    html_pdf = html_driver + html_pax + html_final()
    PDFReport_Name = titles(2)
    pdfkit.from_string(html_pdf, DATA_DIR + PDFReport_Name)

    log.write(str(datetime.now()) +  "-END-COHORT-SAVEPDF\n")
    # SEND MAIL ########################################################################### SEND MAIL #
    log.write(str(datetime.now()) +  "-START-COHORT-SENDMAIL\n")

    html_body = html_inicial() + html_driver + html_pax + html_final()
    Subject = titles(1)
    SendEmail(Subject,html_body,'*****@*****.**','*****@*****.**','123@datateam','',PDFReport_Name)

    log.write(str(datetime.now()) +  "-END-COHORT-SENDMAIL\n")
    ########################################################################
    log.write(str(datetime.now()) + "-END-COHORT\n")
    log.close()
Exemple #23
0
def html2pdf(html_code, file_name):
    '''
    标题和html代码转换成pdf
    '''
#     html = pre_html + body_code + '</body></html>'
    try:
        pdfkit.from_string(html_code, file_name, options=options, css= css_file)
    except  Exception, e:
        print e
Exemple #24
0
def getPdf(html,path):
    head='''
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>

    '''

    pdfkit.from_string(head+html, path[8:])
Exemple #25
0
def toHTML(data, client):

    fn = slugify(client + " "+ datetime.date.today().strftime('%m/%d/%y'))
    jenv = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)
    tmpl = jenv.get_template('template.html')
    out = tmpl.render(data=data, client=client)
    with open('reports/'+fn+'.html', "wb") as fh:
        fh.write(out.encode('utf-8'))
    pdfkit.from_string(out, 'reports/'+fn+'.pdf', css=css)
Exemple #26
0
    def generatePDF(self, url, order):
        ext = tldextract.extract(url)
        domain=  ext.domain+"."+ext.tld
        html = self.opener.open(url).read()
        readable_article = Document(html).summary()
        readable_title = Document(html).short_title()
        self.options["title"] = readable_title

        pdfkit.from_string(readable_article, self.path+"/"+str(order)+"-"+domain+"-"+readable_title+'.pdf', options=self.options)
Exemple #27
0
 def create_pdf(self, fdir, fname, data):
     """Creates pdf file from given string"""
     try:
         pdfkit.from_string(data, os.path.join(fdir, fname))
     except Exception as ex:
         log.exception(ex)
         return False
     log.info("[PDF] Written {0} to {1}".format(len(data), os.path.join(fdir, fname)))   
     return True
Exemple #28
0
 def to_pdf(self,file_path=None,**options):
     """Converts the file to a pdf and saves it at file_path. If file_path is None, it will auto name
     the resulting file to self.path with pdf as the extension"""
     #todo: add toc and other options in wkhtmltopdf
     if file_path is None:
         file_path=change_extension(self.path,"pdf")
     config = pdfkit.configuration(wkhtmltopdf=WKHTML_PATH)
     pdfkit.from_string(str(self),file_path,configuration=config)
     return file_path
 def html_text_to_pdf(html_text, destination_directory, output_file_name):
     file_path = os.path.join(destination_directory, output_file_name)
     if os.path.exists(file_path):
         new_file_path = os.path.join(destination_directory, "Alt" + str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')))
     options = {
         'page-size': 'Letter',
         'dpi': '600'
     }
     pdfkit.from_string(html_text, file_path, options=options)
     return new_file_path
Exemple #30
0
    def write_summary(self, text, file_name):
        """
        Write a summary pdf to the data_dir.
        """
        writer = StringIO()
        writer.write(text)

        pdfkit.from_string(writer.getvalue(), str(self.data_dir) + "/" +
                           str(file_name) + ".pdf")
        writer.close()
Exemple #31
0
    def generate(self):
        filename = 'cert-%s-%s.pdf' % (slugify(
            self.user.username), self.number)
        filepath = '/tmp/%s' % filename
        rendered_html = self.preview()

        options = {
            'page-size': 'A4',
            'orientation': 'Landscape',
            'margin-top': '0.15in',
            'margin-right': '0in',
            'margin-bottom': '0in',
            'margin-left': '0in',
            'no-outline': None,
            'dpi': 300
        }
        pdf = pdfkit.from_string(rendered_html, filepath, options=options)
        response = HttpResponse(pdf, content_type='application/pdf')
        response['Content-Disposition'] = f'attachment; filename={filename}'

        certificate_file = open(filepath, 'rb')
        upload_file = SimpleUploadedFile(filename, certificate_file.read())
        self.certificate_file = upload_file
        self.save()
Exemple #32
0
def report(id):

    order = Order()
    ret = order.get(id)
    if not order.id:
        flash(ret, 'info')
        return redirect(url_for('order.index'))

    order_p = OrderProduct()
    products = order_p.getByOrderId(order.id)
    images = []
    for product in products:
        images.append(b64encode(product[7]).decode("utf-8"))

    ren = render_template('order_report.html',
                          products=products,
                          images=images,
                          order=order)
    pdf = pdfkit.from_string(ren, False)
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers[
        'Content-Disposition'] = 'attachement; filename=relatorio-pedido.pdf'
    return response
Exemple #33
0
def my_order_pdf(event_id, user_id):
    event = Event.query.get_or_404(event_id)
    user = User.query.get_or_404(user_id)
    instance = Instance.query.first()

    if instance.users_can_see_master_overview == False and current_user.id != user.id:
        if current_user.is_admin == False:
            flash('Access denied', 'warning')
            return redirect(url_for('general.home'))

    user_active = is_user_active(user.id, event.id)
    order_object = None
    if user_active == True:
        user_items = fetch_user_items(user.id, event.id)
        order_object = MyOrderObject(user, user_items, event)

    # PDF Setup
    config = pdfkit.configuration(wkhtmltopdf=instance.wkhtmltopdf_path)
    rendered = render_template('my_order.html',
                               is_pdf=True,
                               event=event,
                               user_active=user_active,
                               user_id=user.id,
                               form=None,
                               order_object=order_object,
                               user_name=user.username,
                               title=f"{user.username}'s order")
    pdf = pdfkit.from_string(rendered,
                             False,
                             configuration=config,
                             options={'quiet': ''})
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers[
        'Content-Disposition'] = f'attachment; filename=Order_Overview.pdf'
    return response
Exemple #34
0
def json_to_html(request):
    config = pdfkit.configuration(wkhtmltopdf=PATH_WKHTMLTOPDF)

    if 'data' in request.POST:
        data = request.POST['data']
        json_data = json.loads(data)
        try:
            html = json2html.convert(json=json_data)
            print("================================================")
            print(html)
            print("================================================")
            output = pdfkit.from_string(html,
                                        output_path=False,
                                        configuration=config)
        except:
            print("Json parse error!")
            return HttpResponse("Json parse error!")

        response = HttpResponse(content_type="application/pdf")
        response.write(output)
        return response

    else:
        return HttpResponse('No Content!')
Exemple #35
0
def documenthistory(db, docid):

    html_string = ''

    aux = -1
    end = ''

    query = 'Select * from public.documenthistory(%s,%s,%s)'
    db.execute(query, (1, 100, docid))
    resultados = db.fetchall()
    resultadosJson = json.dumps(resultados, cls=DjangoJSONEncoder)

    text = json.loads(resultadosJson)
    texto = text[0]["docbody"]
    #teste =  texto.encode('latin-1', 'xmlcharrefreplace')

    image = open('parlamento-de-timor.jpg', 'rb')
    img = base64.b64encode(image.read())

    html_template = open('html_model.html', "r")

    template = Template(html_template.read())
    #b = template.render(titulo=text[0]["doctitle"], texto=text[0]["docbody"],image=img.decode(encoding='utf-8'))
    b = template.render(titulo=text[0]["doctitle"],
                        texto=texto,
                        image=img.decode(encoding='utf-8'))

    css = 'teste.css'
    #config = pdfkit.configuration(wkhtmltopdf='C:/tmp/wkhtmltopdf/bin/wkhtmltopdf.exe')
    html_template_and_content = pdfkit.from_string(b, "doc.pdf", css=css)

    #html_template_and_content = HTML(string=b)
    #html_template_and_content.write_pdf('doc.pdf',stylesheets={'teste.css'})

    #return html_template_and_content
    return static_file("doc.pdf", root='')
Exemple #36
0
def invoice_pdf():
    """This generates a PDF of the user's order."""
    response = make_response(
        pdfkit.from_string(
            render_template('checkout/invoice_pdf.html',
                            user_order=session['user_order'],
                            name=session['name'],
                            messages=session['messages'],
                            instructions=session['instructions'],
                            image=os.path.join(current_app.root_path,
                                               'static/img/navbar3.png'),
                            choices=session['choices']),
            False,
            css=[
                os.path.join(current_app.root_path, "static/css/all.css"),
                os.path.join(current_app.root_path,
                             "static/css/bootstrap.min.css"),
                os.path.join(current_app.root_path, "static/css/style.css")
            ]))
    response.headers['Content-Type'] = 'application/pdf'
    response.headers[
        'Content-Disposition'] = f'inline; filename={current_user.username}.pdf'
    pop_invoice_sessions()
    return response
Exemple #37
0
def Sendmail(request, id):
    invoice = Invoice.objects.get(id=id)

    if request.method == 'POST':
        to = request.POST.get('to')
        subject = request.POST.get('subject')
        message = request.POST.get('message')

        EMAIL_ADDRESS = os.environ.get('EMAIL_ADDRESS')
        EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD')

        msg = EmailMessage()
        msg['Subject'] = subject
        msg['From'] = EMAIL_ADDRESS
        msg['To'] = to
        msg.set_content(message)

        template = get_template('sendmailinvoice.html')
        html = template.render({'invoice': invoice})
        options = {
            'page-size': 'A4',
            'encoding': "UTF-8",
        }
        css = 'static/css/invoice.css'
        pdf = pdfkit.from_string(html, False, options, css=css)
        file_name = invoice.site + " scaffold invoice.pdf"

        msg.add_attachment(pdf,
                           maintype='application',
                           subtype='octet-stream',
                           filename=file_name)
        with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
            smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
            smtp.send_message(msg)

    return redirect('invoice:final_invoice', id=invoice.id)
Exemple #38
0
def download_invoice(request, purchase_id):
    # template = get_template('pdf/invoice.html')
    purchase = PurchaseRecord.objects.last(purchase_id=purchase_id)
    if not purchase:
        return HttpResponse(status=404)
    customer = purchase.customer

    p = {
        'id': purchase.id,
        'name': purchase.customer.first_name + ' ' + purchase.customer.surname,
        'customer_id': customer.id,
        'fullname':
        f'{"Mr. " if customer.gender == "Male" else "Ms. "}{str(customer)}',
        'street': customer.street,
        'postcode': customer.postcode,
        'place': customer.place,
        'offer_date': purchase.offer_date,
        'date_sent': purchase.date_sent,
        'price_without_tax': purchase.price_without_tax,
        'tax': "%0.2f" % (purchase.price_without_tax * 0.19),
        'price_with_tax': purchase.price_with_tax,
    }
    context = {'purchase': p}

    rendered = render_to_string('pdf/invoice.html', context, request=request)
    options = {
        'page-size': 'Letter',
        'margin-top': '0.75in',
        'margin-right': '0.75in',
        'margin-bottom': '0.75in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        'no-outline': None
    }
    pdf = pdfkit.from_string(rendered, False, options=options)
    return HttpResponse(pdf, content_type="application/pdf")
Exemple #39
0
def make_slips(id):
    cur = mysql.connection.cursor()
    cur.execute('SELECT * FROM empleados WHERE id= %s', [id])
    data = cur.fetchall()

    now = datetime.datetime.now()

    curb = mysql.connection.cursor()
    curb.execute(
        'SELECT COUNT(*) FROM licencias WHERE empleado = %s AND EXTRACT(MONTH FROM fecha) = %s AND EXTRACT(YEAR FROM fecha) = %s',
        (id, now.month, now.year))
    licencia = curb.fetchall()
    licencias = licencia[0][0] * 47
    minimo = 930
    desig = data[0][8]
    switcher = {"Gerente": 570, "Empleado": 270, "Contratado": 0}
    cargo = switcher.get(desig)
    tIngresos = minimo + cargo
    afp = (10 / 100) * tIngresos
    tdescuentos = afp + licencias
    tneto = tIngresos - tdescuentos
    rendered = render_template('planilla.html',
                               employee=data[0],
                               minimo=minimo,
                               cargo=cargo,
                               tIngresos=tIngresos,
                               afp=afp,
                               licencias=licencias,
                               tdescuentos=tdescuentos,
                               tneto=tneto,
                               fecha=now)
    pdf = pdfkit.from_string(rendered, False)
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=planilla.pdf'
    return response
def driver_employ():
    u_name = request.args.get('user_name')
    if u_name:
        user = DriversData.objects(user_name=u_name).first()
        if not user:
            return jsonify({'error': 'User Name Not Found'})
        data = form_i9_data(user)
        options = {
            'page-size': 'A4',
            'encoding': 'utf-8',
            'margin-top': '1.8cm',
            'margin-bottom': '0cm',
            'margin-left': '0.5cm',
            'margin-right': '0.5cm',
            "enable-local-file-access": ""
        }
        data['formi9'] = False
        try:
            html = render_template("style_css.html", data=data)
            pdf = pdfkit.from_string(html,
                                     False,
                                     options=options,
                                     configuration=PDFKIT_CONFIGURATION)
            resp = make_response(pdf)
            resp.headers['Content-Type'] = 'application/pdf'
            resp.headers[
                'Content-Disposition'] = 'attachment; filename=Driver Employ' + u_name + '.pdf'
            return resp
        except:
            return jsonify({
                'error':
                'Unable To Make Pdf Please Fill The Data Correctly' + u_name
            })

    else:
        return jsonify({'error': 'Invalid Data'})
Exemple #41
0
def resume():
    aboutme = AboutMe.query.first()
    achievements = Achievement.query.all()
    projects = Project.query.filter_by(featured=True).all()
    tags = Tag.query.all()
    fluent_tags = Tag.query.filter_by(knowledge='fluent').all()
    proficient_tags = Tag.query.filter_by(knowledge='proficient').all()
    familiar_tags = Tag.query.filter_by(knowledge='familiar').all()

    options = {
        'page-size': 'A4',
        # 'encoding': "UTF-8",
        # 'margin-top': '0.5in',
        # 'margin-right': '0.5in',
        # 'margin-bottom': '0.5in',
        # 'margin-left': '0.5in',
        # 'no-outline': None,
        # 'no-background': True
    }

    # options = {'disable-javascript': True}

    resume_html = render_template('resume.html',
                                  aboutme=aboutme,
                                  achievements=achievements,
                                  projects=projects,
                                  fluent_tags=fluent_tags,
                                  proficient_tags=proficient_tags,
                                  familiar_tags=familiar_tags)
    pdf = pdfkit.from_string(resume_html, False, options=options)

    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=MaxBender.pdf'

    return response
def form_to_PDF(request, donor_id=1):

    donation_request = DonationRequests.objects.get(id=donor_id)
    user = donation_request.donor
    donations = DonationRequests.objects.filter(donor=user)
    template = get_template("user-details.html")
    html = template.render({'user': user, 'donors': donations})
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF)
    try:
        pdf = pdfkit.from_string(html, False, configuration=config)
    except Exception as e:
        print(e)
        pass
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'inline; filename="report.pdf"'
    userpdf = PdfFileReader(BytesIO(pdf))
    usermedicaldoc = donation_request.upload_medical_doc.read()
    usermedbytes = BytesIO(usermedicaldoc)
    usermedicalpdf = PdfFileReader(usermedbytes)
    merger = PdfFileMerger()
    merger.append(userpdf)
    merger.append(usermedicalpdf)
    merger.write(response)
    return response
Exemple #43
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options,
                                             request=None,
                                             user=user,
                                             finding_notes=finding_notes,
                                             finding_images=finding_images,
                                             host=host)

    widgets = selected_widgets.values()
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth

            toc_bytes = render_to_string(
                xsl_style_sheet_tempalte, {
                    'widgets': widgets,
                    'depth': toc_depth,
                    'title': toc_settings.title
                })

            temp.write(toc_bytes)
            temp.seek(0)

            toc = {
                'toc-header-text': toc_settings.title,
                'xsl-style-sheet': temp.name
            }

        cover = None
        if 'cover-page' in selected_widgets:
            cp = selected_widgets['cover-page']
            x = urlencode({
                'title': cp.title,
                'subtitle': cp.sub_heading,
                'info': cp.meta_info
            })
            cover = host + reverse('report_cover_page') + "?" + x

        bytes = render_to_string(
            template, {
                'widgets': widgets,
                'toc_depth': toc_depth,
                'host': host,
                'report_name': report.name
            })

        pdf = pdfkit.from_string(
            bytes,
            False,
            configuration=config,
            cover=cover,
            toc=toc,
        )

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = datetime.now(tz=localtz)
        report.save()
        # email_requester(report, uri)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        raise e
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True
Exemple #44
0
    def get(self, request, *args, **kwargs):
        options = {
            'encoding': 'utf-8',
        }
        try:
            mail_ids = kwargs.get('mail_ids')

            mails = []
            for m_id in mail_ids.split(','):
                m_id = from_global_id(m_id)[1]
                mails.append(Mail.objects.get(pk=m_id))

            mail_count = len(mails)

            if mail_count == 0:
                return HttpResponse('')
            elif mail_count == 1:
                mail = mails[0]
                data = get_mail_data(mail)
                mail_content = render_to_string('pdf/mail.html', data)

                tmp_filename = 'media/{}.pdf'.format(uuid.uuid4().hex)
                pdfkit.from_string(unescape(mail_content),
                                   tmp_filename,
                                   options=options)

                file_data = None
                with open(tmp_filename, 'rb') as f:
                    file_data = f.read()

                filename = get_mail_filename(data, single=True)
                response = HttpResponse(content_type='application/pdf')
                response[
                    'Content-Disposition'] = 'attachment; filename="{}"'.format(
                        filename)
                response.write(file_data)

                os.remove(tmp_filename)

                return response
            else:
                tmp_folder = uuid.uuid4().hex
                os.makedirs('/mails/{}'.format(tmp_folder), exist_ok=True)

                for mail in mails:
                    data = get_mail_data(mail)
                    mail_content = render_to_string('pdf/mail.html', data)
                    filename = '/mails/{}/{}'.format(tmp_folder,
                                                     get_mail_filename(data))
                    pdfkit.from_string(unescape(mail_content),
                                       filename,
                                       options=options)

                zip_filename = '/mails/mails.zip'

                zip_folder('/mails/{}/'.format(tmp_folder), zip_filename)

                zip_data = None

                with open(zip_filename, 'rb') as f:
                    zip_data = f.read()

                response = HttpResponse(content_type='application/zip')
                response[
                    'Content-Disposition'] = 'attachment; filename="mails.zip"'
                response.write(zip_data)

                shutil.rmtree('/mails')

                return response
        except Exception as e:
            print(str(e))
            return HttpResponse('')
Exemple #45
0
def main():
    " Main loop of the programm "

    # Load mail host
    mail = imaplib.IMAP4_SSL(HOST, PORT)
    # Login user
    mail.login(USER, PASS)
    #Select mailbox
    mail.select(MAILBOX)
    # Selection for specific emails
    _, selected_mails = mail.search(None, FROM, SUBJECT)

    # Loop over all the found emails
    for selected_mail_data in selected_mails[0].split():
        # Fetch and decode message
        _, mess = mail.fetch(selected_mail_data, "RFC822")
        message = email.message_from_string(mess[0][1].decode("utf-8"))

        # Get barcode of current email message (for saves later)
        barcode = re.search(re.compile(BARCODE_REGEX),
                            message["Subject"]).group(0)

        # Split the email in parts
        # All image data in the email will be saved in image_ids
        # All html data in the email will be saved in html_data
        image_ids = {}
        html_data = []
        for part in message.walk():

            # Receive decoded payload of the message part
            payload = part.get_payload(decode=True)

            # Save the image source code with the cid as key
            if "image" in part["Content-Type"]:
                image_ids[part["Content-ID"]] = "data:" + part[
                    "Content-Type"].split(";")[0]
                image_ids[part["Content-ID"]] += ";base64,"
                image_ids[part["Content-ID"]] += base64.b64encode(
                    payload).decode("utf-8")

            # Check if the message part is an attachment
            # also select for the word 'bon' is in the filename (Specific for a IKEA delivery)
            # Finally, save the attachment in the saving path
            if part.get_content_maintype != "multipart" and \
               part.get("Content-Disposition") is not None and \
               ATTACH_PREFIX in part.get_filename():
                save_file(payload,
                          os.path.join(SAVING_PATH + barcode + "_bon.pdf"))

            # Check if there is text in the email and add it to the html string
            if part.get_content_type() == "text/html":
                html_data.append(payload.decode("utf-8"))

        # Match the cid in the html code with the cid key from the image dict.
        # Replace the html src with the image source code
        for html_str in html_data:
            for image_id in image_ids:
                image_loc_in_html = re.search(image_id[1:-1], html_str)
                image_html_str = html_str[image_loc_in_html.start() -
                                          4:image_loc_in_html.end()]
                html_str = re.sub(image_html_str, image_ids[image_id],
                                  html_str)
            ##save the complete html as a html file
            pdfkit.from_string(
                html_str, os.path.join(SAVING_PATH + barcode + "_mail.pdf"))
Exemple #46
0
 def exportEvalPDF(self):
     pdfkit.from_string(self.html, str(datetime.date.today()) + ".pdf")
Exemple #47
0
    def download_report(self, request, *args, **kwargs):
        audit_job = self.get_object()
        report_data = get_report_by_audit(audit_job)
        options = {
            'page-size': 'A4',
            'margin-top': '0.75in',
            'margin-right': '0.75in',
            'margin-bottom': '0.75in',
            'margin-left': '0.75in',
            'encoding': 'UTF-8'
        }
        trs = ''
        details = ''
        conclusion = report_data.get('conclusion', '')
        data_list = report_data.get('data', '')
        for data in data_list:
            name = ''
            if data.get('result'):
                name = f'''href="#{(data.get('name'))}"'''
            trs += f'''
            
                    <tr>
                        <td style="text-align:center;">
                            {(data.get('audit_type', ''))}<br />
                        </td>
                        <td style="text-align:center;">
                            {(data.get('target', ''))}<br />
                        </td>
                        <td style="text-align:center;">
                            <a {name}>{(data.get('name', ''))}</a><br />
                        </td>
                        <td style="text-align:center;">
                            {(data.get('score', ''))}<br />
                        </td>
                        <td style="text-align:center;">
                            {(data.get('problem', ''))}<br />
                        </td>
                        <td style="text-align:center;">
                            {(data.get('total', ''))}<br />
                        </td>
                        <td style="text-align:center;">
                            {(data.get('problem_rate', ''))}%<br />
                        </td>
                    </tr>        
            '''
            detail_head = ''
            detail_tr = ''
            for result in data.get('result', []):
                detail_td = ''
                head_td = ''
                for k, v in result.items():
                    head_td += f'''
                            <td style="text-align:center;">
                            {k}<br />
                            </td>
                            '''
                    detail_head = f'''
                                <tr>
                                    {head_td}
                                </tr>
                            '''
                    detail_td += f'''
                            <td style="text-align:center;">
                            {v}
                            </td>
                            '''

                detail_tr += f'''
                            <tr>
                                {detail_td}
                            </tr>
                            '''

            if data.get('result'):
                details += f'''
                    <div id="{(data.get('name', ''))}">
                        <span style="font-size:18px;">{(data.get('name', ''))}:</span></br>
                        <table cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
                            <tbody>
                                {detail_head}
                                {detail_tr}
                            </tbody>
                        </table>
                        </br></br>
                    </div>
                    '''

        html = f'''
                    <p>
                        <span><br />
                        <h1 style="text-align:center;">
                            <span style="color:#000000;font-family:SimSun;">{(audit_job.name)}</span>
                        </h1>
                        <p>
                            <span style="color:#000000;font-family:SimSun;"><br />
                    </span>
                        </p>
                        <p>
                            <span style="color:#000000;font-family:SimSun;"><br />
                    </span>
                        </p>
                    </span><span><span style="font-size:18px;">:</span></span>
                    </p>
                    <p>
                        <span>
                    <pre>{conclusion}</pre>
                        <div class="ant-table-wrapper _3HTnYrvUcsaZCJOoIk5Wqr" style="margin:1rem 0px 0px;padding:0px;color:rgba(0, 0, 0, 0.65);font-family:&quot;background-color:#FFFFFF;">
                            <div class="ant-spin-nested-loading" style="margin:0px;padding:0px;">
                                <div class="ant-spin-container" style="margin:0px;padding:0px;">
                                    <div class="ant-table ant-table-small ant-table-bordered ant-table-scroll-position-left" style="margin:0px;padding:0px;">
                                        <div class="ant-table-content" style="margin:0px;padding:0px;">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <br />
                    <span style="font-size:18px;">:</span></span>
                    </p>
                    <p>
                        <br />
                    </p>
                    <p>
                        <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000" align="center">
                            <tbody>
                                <tr>
                                    <td style="text-align:center;">
                                        <span style="background-color:;"></span><br />
                                    </td>
                                    <td style="text-align:center;">
                                        <span style="color:rgba(0, 0, 0, 0.85);font-family:&quot;background-color:#FFFFFF;">target</span><br />
                                    </td>
                                    <td style="text-align:center;">
                                        
                                    </td>
                                    <td style="text-align:center;">
                                        
                                    </td>
                                    <td style="text-align:center;">
                                        
                                    </td>
                                    <td style="text-align:center;">
                                        
                                    </td>
                                    <td style="text-align:center;">
                                        
                                    </td>
                                </tr>
                                {trs}
                            </tbody>
                        </table>
                    </p>
                    <br />
                    <br />  
                    {details}
      '''
        pdf = pdfkit.from_string(html, False, options=options)
        response = HttpResponse(content_type='application/pdf')
        response[
            'Content-Disposition'] = 'attachment; filename="yunquReport.pdf"'
        response.write(pdf)
        return response
Exemple #48
0
def main():
    parser = argparse.ArgumentParser(description='Bleju faktury z jsonu.')

    parser.add_argument('json_file', help='jmeno souboru kde mas json')
    parser.add_argument('-o', '--output', help='kam to chces prdnout.')
    parser.add_argument('-t',
                        '--template',
                        help='vlastni jinja2 template na generovani faktury.')
    parser.add_argument('-i',
                        '--info_file',
                        help='soubor z informacema o tobe',
                        default='./me.json')
    parser.add_argument('-f',
                        '--force-overwrite',
                        help='proste to prepis',
                        action="store_true")
    parser.add_argument('--html', help='html dump')

    args = parser.parse_args()

    if not os.path.isfile(args.json_file):
        fuckem('{} neexistuje, kokote.'.format(args.json_file))
    if not os.path.isfile(args.info_file):
        print('{} neexistuje, pyco.'.format(args.info_file))
        print('Ten soubor je potreba, tam strkam tvoje data, aby ses nemusel '
              'furt opakovat dokola, to by bylo na hovno. '
              'Ale vis co? Muzeme ten soubor vytvorit spolu. To bude prima '
              'zabava. Ted se te teda budu ptat na nejaky radoby vtipny '
              'otazky, kdyz neco poseres tak to pak muzes opravit v tom '
              'souboru. Super co?')

        if input("Chces ho vytahnout z ARESu? Ano/Ne") in ('Ano', 'ANO', 'A',
                                                           'a'):
            with open(args.info_file, 'w') as file:
                info = ask_stupid_questions()
                file.write(json.dumps(info, indent=4))
        else:
            fuckem('Budes si ten soubor muset vytvorit sam.')
    else:
        info = load_data(args.info_file)

    if not args.output:
        output_file = os.path.splitext(args.json_file)[0] + '.pdf'
    else:
        output_file = args.output

    if os.path.exists(output_file) and not args.force_overwrite:
        fuckem("Uz existuje, posrals to.")

    invoice_data = clean_invoice_data(load_data(args.json_file))

    template_file = args.template
    if not template_file:
        local_template = os.path.join(os.path.dirname(args.json_file),
                                      'template.html')
        if os.path.exists(local_template):
            template_file = local_template

    html_content = render_template(invoice_data, info, filename=template_file)

    if args.html:
        with open(args.html, 'w') as f:
            f.write(html_content)

    pdfkit.from_string(html_content, output_path=output_file)
Exemple #49
0
import pdfkit

url = 'https://www.cnblogs.com/xiaokuangnvhai/p/11676068.html'
confg = pdfkit.configuration(
    wkhtmltopdf=r'C:\zwj\besttest\wkhtmltox\bin\wkhtmltopdf.exe')

pdfkit.from_url(url, 'aa.pdf', configuration=confg)  #URL转换PDF

pdfkit.from_file('my.html', 'aa.pdf', configuration=confg)  #文件转换PDF

html = ''
pdfkit.from_string(html, 'aa.pdf', configuration=confg)  #字符串转换PDF
pdfkit.from_string(html, 'aa.pdf', configuration=confg)  #字符串转换PDF
Exemple #50
0
def finduplicate(request):
    allproject = ProjectDetail.objects.all()
    fields_to_end_with_br = [
        'genres', 'locations', 'producers', 'writers', 'directors', 'cast'
    ]
    fields_to_join = [
        (',', 'plot'),
        (' -', 'cast'),
        (',', 'genres'),
        (' -', 'studios'),
        (' -', 'writers'),
        (' -', 'directors'),
        (' -', 'producers'),
        (' -', 'locations'),
        (',', 'production_companies'),
    ]
    html = ""
    head = ""
    i = 0
    for item in allproject:
        item = model_to_dict(
            item, fields=[field.name for field in item._meta.get_fields()])
        movie = item.copy()

        for joiner, field in fields_to_join:
            movie[field] = join_fields(movie[field], "-")

        for field in fields_to_end_with_br:
            if movie[field]:
                movie[field] = f'{movie[field]}<br>'

        name_hash = hashlib.sha224(str(
            item['title']).encode('utf-8')).hexdigest()[:8]
        file_name = f'{name_hash}.pdf'
        file_path = f'{FILE_PATH}/{file_name}'

        movie['listing'] = name_hash

        movie = fix_movie_info(movie)
        if i == 0:
            head = header.format(**movie)
        i += 1
        c = html_format_t.format(**movie)
        html = html + c
    html = head + html + footer
    pdfkit.from_string(html, file_path)
    # dupes = ProjectDetail.objects.values('title').annotate(Count('id')).order_by().filter(id__count__gt=1)
    # print(dupes)
    # # m=Movies.objects.filter(title__in=[item['title'] for item in dupes])
    # # print(m)
    # jsonDec = json.decoder.JSONDecoder()
    # a=ProjectDetail.objects.get(title='The Banishing')
    # print(type(a.production_companies))
    # data=a.production_companies.strip('][').split(', ')
    # contactemail=[]
    # contactemailphone = []
    # fax=[]
    #
    # i=0
    # for text in data:
    #     text=text.replace(": ","")
    #     try:
    #
    #         if 'Email' in text:
    #             contactemail.append(data[i+2])
    #         if 'Phone' in text:
    #             contactemailphone.append(data[i+1])
    #         if 'Fax' in text:
    #             fax.append(data[i+1])
    #     except:
    #         print("no")
    #     i+=1
    # print(contactemail)
    # print(contactemailphone)
    # print(fax)
    # data=list(set(data)-set(contactemail))
    # data = list(set(data) - set(contactemailphone))
    # data = list(set(data) - set(fax))
    # print(data)
    # for text in data:
    #     text=text.replace(":","").strip()
    #     text=text.strip()
    #     print(text)
    #     for groups in phoneRegex.findall(text):
    #         print("okk")
    #         print(groups)

    # emptyfield1=[]
    # emptyfield2=[]
    # for feild in ProjectDetail._meta.get_fields():
    #     if getattr(obj1,feild.name)==None or getattr(obj1,feild.name)==[] or getattr(obj1,feild.name)=='[""]' or getattr(obj1,feild.name)=="":
    #         print(feild.name)
    #
    # obj1.save()
    return HttpResponse(request, 200)
Exemple #51
0
        print(i)

exit()

# 配置PDF选项 避免中文乱码
options = {
    'page-size': 'Letter',
    'encoding': "UTF-8",
    'custom-header': [('Accept-Encoding', 'gzip')]
}
fileName = r"r:\a.pdf"
file = open(r'r:\a.html', 'rt', encoding='utf-8')
origHtml = file.read()
file.close()
origHtml = origHtml.replace(r'background:#000', r'background:#fff')
pdfkit.from_string(origHtml, fileName, options=options)
print('Done')
exit()

f = open('r:\\1.py', 'rt', encoding='UTF-8')
ers = f.read()
f.close()
ers = ers.replace(chr(8203), '')
for s in ers:
    print("%s-->%s" % (s, ord(s)))
print(ers)
exit()

url = "https://leetcode-cn.com/problems/two-sum/"
html = urlopen(url)
bs = BeautifulSoup(html, "html.parser")
# -*- coding: utf-8 -*-
"""
Created on 2017/10/26
@author: MG
"""

import pdfkit
pdfkit.from_url('http://www.baidu.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
Exemple #53
0
def convert_pdf(decrypted_html, new_pdf_file, config=None):
    return pdfkit.from_string(decrypted_html,
                              new_pdf_file,
                              configuration=config)
def pdf(request, api=False):
    try:
        if api:
            checksum = request.POST['hash']
            scan_type = request.POST['scan_type']
        else:
            checksum = request.GET['md5']
            scan_type = request.GET['type']
        hash_match = re.match('^[0-9a-f]{32}$', checksum)
        if hash_match:
            if scan_type.lower() in ['apk', 'andzip']:
                static_db = StaticAnalyzerAndroid.objects.filter(MD5=checksum)
                if static_db.exists():
                    print "\n[INFO] Fetching data from DB for PDF Report Generation (Android)"
                    context = get_context_from_db_entry(static_db)
                    if scan_type.lower() == 'apk':
                        template = get_template("pdf/static_analysis_pdf.html")
                    else:
                        template = get_template(
                            "pdf/static_analysis_zip_pdf.html")
                else:
                    if api:
                        return {"report": "Report not Found"}
                    else:
                        return HttpResponse(
                            json.dumps({"report": "Report not Found"}),
                            content_type="application/json; charset=utf-8",
                            status=500)
            elif re.findall('ipa|ioszip', scan_type.lower()):
                if scan_type.lower() == 'ipa':
                    static_db = StaticAnalyzerIPA.objects.filter(MD5=checksum)
                    if static_db.exists():
                        print "\n[INFO] Fetching data from DB for PDF Report Generation (IOS IPA)"
                        context = get_context_from_db_entry_ipa(static_db)
                        template = get_template(
                            "pdf/ios_binary_analysis_pdf.html")
                    else:
                        if api:
                            return {"report": "Report not Found"}
                        else:
                            return HttpResponse(
                                json.dumps({"report": "Report not Found"}),
                                content_type="application/json; charset=utf-8",
                                status=500)
                elif scan_type.lower() == 'ioszip':
                    static_db = StaticAnalyzerIOSZIP.objects.filter(
                        MD5=checksum)
                    if static_db.exists():
                        print "\n[INFO] Fetching data from DB for PDF Report Generation (IOS ZIP)"
                        context = get_context_from_db_entry_ios(static_db)
                        template = get_template(
                            "pdf/ios_source_analysis_pdf.html")
                    else:
                        if api:
                            return {"report": "Report not Found"}
                        else:
                            return HttpResponse(
                                json.dumps({"report": "Report not Found"}),
                                content_type="application/json; charset=utf-8",
                                status=500)
            elif re.findall('appx', scan_type.lower()):
                if scan_type.lower() == 'appx':
                    db_entry = StaticAnalyzerWindows.objects.filter(  # pylint: disable-msg=E1101
                        MD5=checksum)
                    if db_entry.exists():
                        print "\n[INFO] Fetching data from DB for PDF Report Generation (APPX)"

                        context = {
                            'title':
                            db_entry[0].TITLE,
                            'name':
                            db_entry[0].APP_NAME,
                            'pub_name':
                            db_entry[0].PUB_NAME,
                            'size':
                            db_entry[0].SIZE,
                            'md5':
                            db_entry[0].MD5,
                            'sha1':
                            db_entry[0].SHA1,
                            'sha256':
                            db_entry[0].SHA256,
                            'bin_name':
                            db_entry[0].BINNAME,
                            'version':
                            db_entry[0].VERSION,
                            'arch':
                            db_entry[0].ARCH,
                            'compiler_version':
                            db_entry[0].COMPILER_VERSION,
                            'visual_studio_version':
                            db_entry[0].VISUAL_STUDIO_VERSION,
                            'visual_studio_edition':
                            db_entry[0].VISUAL_STUDIO_EDITION,
                            'target_os':
                            db_entry[0].TARGET_OS,
                            'appx_dll_version':
                            db_entry[0].APPX_DLL_VERSION,
                            'proj_guid':
                            db_entry[0].PROJ_GUID,
                            'opti_tool':
                            db_entry[0].OPTI_TOOL,
                            'target_run':
                            db_entry[0].TARGET_RUN,
                            'files':
                            python_list(db_entry[0].FILES),
                            'strings':
                            python_list(db_entry[0].STRINGS),
                            'bin_an_results':
                            python_list(db_entry[0].BIN_AN_RESULTS),
                            'bin_an_warnings':
                            python_list(db_entry[0].BIN_AN_WARNINGS)
                        }
                        template = get_template(
                            "pdf/windows_binary_analysis_pdf.html")
            else:
                if api:
                    return {"scan_type": "Type is not Allowed"}
                else:
                    return HttpResponse(
                        json.dumps({"type": "Type is not Allowed"}),
                        content_type="application/json; charset=utf-8",
                        status=500)

            context['VT_RESULT'] = None
            if settings.VT_ENABLED:
                app_dir = os.path.join(settings.UPLD_DIR, checksum + '/')
                vt = VirusTotal.VirusTotal()
                context['VT_RESULT'] = vt.get_result(
                    os.path.join(app_dir, checksum) + '.' + scan_type.lower(),
                    checksum)

            html = template.render(context)
            try:
                options = {
                    'page-size': 'A4',
                    'quiet': '',
                    'no-collate': '',
                    'margin-top': '0.50in',
                    'margin-right': '0.50in',
                    'margin-bottom': '0.50in',
                    'margin-left': '0.50in',
                    'encoding': "UTF-8",
                    'custom-header': [('Accept-Encoding', 'gzip')],
                    'no-outline': None
                }

                BASE_DIR = os.path.dirname(
                    os.path.dirname(os.path.dirname(
                        os.path.abspath(__file__))))
                path_wk = os.path.join(BASE_DIR,
                                       'windows/wkhtmltopdf/wkhtmltopdf.exe')
                # path_wk = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'  # 安装位置
                config = pdfkit.configuration(wkhtmltopdf=path_wk)
                pdf_dat = pdfkit.from_string(html,
                                             False,
                                             options=options,
                                             configuration=config)
                if api:
                    return {"pdf_dat": pdf_dat}
                else:
                    return HttpResponse(pdf_dat,
                                        content_type='application/pdf')
            except Exception as exp:
                if api:
                    return {
                        "error": "Cannot Generate PDF",
                        "err_details": str(exp)
                    }
                else:
                    return HttpResponse(
                        json.dumps({
                            "pdf_error": "Cannot Generate PDF",
                            "err_details": str(exp)
                        }),
                        content_type="application/json; charset=utf-8",
                        status=500)

        else:
            if api:
                return {"error": "Invalid scan hash"}
            else:
                return HttpResponse(
                    json.dumps({"md5": "Invalid MD5"}),
                    content_type="application/json; charset=utf-8",
                    status=500)
    except Exception as exp:
        msg = str(exp)
        exp = exp.__doc__
        if api:
            return print_n_send_error_response(request, msg, True, exp)
        else:
            return print_n_send_error_response(request, msg, False, exp)
Exemple #55
0
# https://stackoverflow.com/questions/23359083/how-to-convert-webpage-into-pdf-by-using-python
# https://pypi.org/project/pdfkit/

# brew install Caskroom/cask/wkhtmltopdf
# pip install pdfkit

import pdfkit
pdfkit.from_url('https://www.google.com', 'website-example.pdf')
pdfkit.from_file('test.html', 'file-example.pdf')
pdfkit.from_string('Hello!', 'string-example.pdf')
Exemple #56
0
def gen_pdf(content, name="out.pdf"):
    pdfkit.from_string(content, os.path.join(g_cur_path, 'out.pdf'))
Exemple #57
0
def async_custom_pdf_report(self,
                            report=None,
                            template="None",
                            filename='report.pdf',
                            host=None,
                            user=None,
                            uri=None,
                            finding_notes=False,
                            finding_images=False):
    config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)

    selected_widgets = report_widget_factory(json_data=report.options, request=None, user=user,
                                             finding_notes=finding_notes, finding_images=finding_images, host=host)

    widgets = list(selected_widgets.values())
    temp = None

    try:
        report.task_id = async_custom_pdf_report.request.id
        report.save()

        toc = None
        toc_depth = 4

        if 'table-of-contents' in selected_widgets:
            xsl_style_sheet_tempalte = "dojo/pdf_toc.xsl"
            temp = tempfile.NamedTemporaryFile()

            toc_settings = selected_widgets['table-of-contents']

            toc_depth = toc_settings.depth
            toc_bytes = render_to_string(xsl_style_sheet_tempalte, {'widgets': widgets,
                                                                    'depth': toc_depth,
                                                                    'title': toc_settings.title})
            temp.write(toc_bytes)
            temp.seek(0)

            toc = {'toc-header-text': toc_settings.title,
                   'xsl-style-sheet': temp.name}

        # default the cover to not come first by default
        cover_first_val = False

        cover = None
        if 'cover-page' in selected_widgets:
            cover_first_val = True
            cp = selected_widgets['cover-page']
            x = urlencode({'title': cp.title,
                           'subtitle': cp.sub_heading,
                           'info': cp.meta_info})
            cover = host + reverse(
                'report_cover_page') + "?" + x
        bytes = render_to_string(template, {'widgets': widgets,
                                            'toc_depth': toc_depth,
                                            'host': host,
                                            'report_name': report.name})
        pdf = pdfkit.from_string(bytes,
                                 False,
                                 configuration=config,
                                 toc=toc,
                                 cover=cover,
                                 cover_first=cover_first_val)

        if report.file.name:
            with open(report.file.path, 'w') as f:
                f.write(pdf)
            f.close()
        else:
            f = ContentFile(pdf)
            report.file.save(filename, f)
        report.status = 'success'
        report.done_datetime = timezone.now()
        report.save()

        create_notification(event='report_created', title='Report created', description='The report "%s" is ready.' % report.name, url=uri, report=report, objowner=report.requester)
    except Exception as e:
        report.status = 'error'
        report.save()
        # email_requester(report, uri, error=e)
        # raise e
        log_generic_alert("PDF Report", "Report Creation Failure", "Make sure WKHTMLTOPDF is installed. " + str(e))
    finally:
        if temp is not None:
            # deleting temp xsl file
            temp.close()

    return True
Exemple #58
0
files = {}
pbar = tqdm(total=len(modulosIncluidos), unit='Mods')
mergerSprint = PdfFileMerger()
for modName in modulosIncluidos:
    files[modName] = []
    for root, dirnames, filenames in os.walk(myDir):
        files[modName].extend(glob.glob(root + "/" + modName + "*.html"))
    files[modName] = sorted(files[modName], reverse=True)

    for _file in files[modName]:
        if not Path(_file + '.pdf').is_file() or True:
            html = '' + pre_html
            html += open(_file, 'r').read()
            html = html.decode('utf-8')
            pbar.write(_file + '.pdf')
            pdfkit.from_string(html, _file + '.pdf', {'quiet': ''})

    merger = PdfFileMerger()
    for _file in files[modName]:
        p = Path(_file)
        pdfName = str(p.parent.joinpath(p.stem + '.html.pdf'))
        merger.append(open(pdfName, 'rb'), import_bookmarks=False)

    with open(modName + '.pdf', "wb") as fout:
        merger.write(fout)
        pbar.update(1)
        pbar.write(modName + '.pdf')

    mergerSprint.append(open(modName + '.pdf', 'rb'), import_bookmarks=False)

ultimateName = 'Sprint5.pdf'
Exemple #59
0
            logging.warning('Content is none when converting nid:' + nid)
        resHtml = str(title) + str(content)

        # Create directory if it is not existed
        try:
            os.stat(directory)
        except:
            try:
                os.makedirs(directory)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
        # Convert webpage using pdf kit module
        print("Converting " + file_path)
        try:
            pdfkit.from_string(resHtml, file_path, options=PDF_OPTIONS)
        except OSError:
            logging.warning('OSError when converting nid:' + nid)
        except Exception as e:
            raise

        # Download attachments if there are any
        if (nid in nid_dict):
            # Create attachments directory
            attachment_dir = OUTPUT_PATH_PREFIX + dirname + ATTACHMENT_DIR_SUFFIX
            try:
                os.stat(attachment_dir)
            except:
                try:
                    os.makedirs(attachment_dir)
                except OSError as e:
    def get_pdf(self, html_code, title):
        """  html转pdf  生成pdf,这里使用的库是 pdfkit 基于本地 wkhtmltopdf 程序
        :param html_code:
        :param title:
        :return:
        """

        self.pdf_dir_path = os.path.join(self.column_file_path,
                                         'pdf')  # 存放markdown文件的文件夹路径
        if not os.path.exists(self.pdf_dir_path):
            os.makedirs(self.pdf_dir_path)

        options = {
            # 'page-size': 'A4',       # A4(default), Letter(书信大小), A0,A1,B1,etc(等等).
            'page-size': 'Letter',
            'margin-top': '0.75in',
            'margin-right': '0.75in',
            'margin-bottom': '0.75in',
            'margin-left': '0.75in',
            'minimum-font-size': '30',  # 页面字体大小
            'encoding': "UTF-8"  # 设置为utf-8编码,避免生成pdf中文乱码
        }
        options_str = ''
        for each in options:
            options_str += f"--{each} {options[each]} "
        pdf_file_path = os.path.join(self.pdf_dir_path, title + '.pdf')
        try:
            if not os.path.exists(pdf_file_path):
                column_style_file_path = os.path.join(os.getcwd(), 'static',
                                                      'column_style.css')
                column_css = [
                    column_style_file_path,
                ]
                html_code += '博客:http://zwjjiaozhu.top'  # 可以添加自己的ip

                # pdfkit在mac下有点问题,打包后的软件总是提示找不到wxhtmltopdf的程序,然后直接pycharm运行就没问题!
                wkthmltopdf_file_path = os.path.join(os.getcwd(), 'static',
                                                     'wkhtmltopdf')
                # if sys.platform == 'win32':
                #     wkthmltopdf_file_path = subprocess.Popen(
                #         ['where', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
                # else:
                #     wkthmltopdf_file_path = subprocess.Popen(
                #         ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
                # self.down_log(wkthmltopdf_file_path)
                if not wkthmltopdf_file_path:
                    self.down_log(
                        f"{title}: 转换pdf失败,原因:未检测到wkhtmltopdf程序,"
                        f"请到以下网址进行下载安装:https://wkhtmltopdf.org/downloads.html")
                    return ''
                config = pdfkit.configuration(
                    wkhtmltopdf=wkthmltopdf_file_path)
                pdfkit.from_string(html_code,
                                   pdf_file_path,
                                   options=options,
                                   css=column_css,
                                   configuration=config)
                self.down_log(f'{title}:pdf格式下载完毕')
            else:
                self.down_log(f'{title}:pdf格式已存在')
        except Exception as e:
            self.down_log(str(e))