Exemplo n.º 1
0
def vir_typer_results(request, vir_typer_pk):
    from django.core.files.storage import FileSystemStorage
    from django.http import HttpResponse
    from django.template.loader import render_to_string
    import datetime
    manager_codes = {
        'NOVI': {
            'code':
            'NOV1-SEQ',
            'comments_en':
            'Norovirus genogroup I was confirmed by gene sequencing of the amplicon.',
            'comments_fr':
            'Le séquençage de l\'amplicon confirme la présence du Norovirus génogroup I.'
        },
        'NOVII': {
            'code':
            'NOV2-SEQ',
            'comments_en':
            'Norovirus genogroup II was confirmed by gene sequencing of the amplicon.',
            'comments_fr':
            'Le séquençage de l\'amplicon confirme la présence du Norovirus génogroup II.'
        },
        'HAV': {
            'code':
            'HAV-SEQ',
            'comments_en':
            'Hepatitis A was confirmed by gene sequencing of the amplicon.',
            'comments_fr':
            'Le séquençage de l\'amplicon confirme la présence du Hepatitis A.'
        },
        'MNV': {
            'code':
            'MNV-SEQ',
            'comments_en':
            'Murine norovirus was confirmed by gene sequencing of the amplicon.',
            'comments_fr':
            'Le séquençage de l\'amplicon confirme la présence du Murine norovirus.'
        },
    }
    vir_typer_project = get_object_or_404(VirTyperProject, pk=vir_typer_pk)

    vir_typer_samples = list(
        VirTyperRequest.objects.filter(project_name__pk=vir_typer_pk))
    vir_typer_result = list()
    parse = False
    for sample in vir_typer_samples:
        vir_files = list(
            VirTyperFiles.objects.filter(sample_name__pk=sample.pk))
        for vir_file in vir_files:
            try:
                vir_typer_result.append(
                    VirTyperResults.objects.get(sequence_file__pk=vir_file.pk))
            except:
                parse = True
    # Parse the JSON report, and enter the results into the database if necessary
    if parse:
        parse_report(vir_typer_json=vir_typer_project.report,
                     vir_typer_samples=vir_typer_samples)
    vir_typer_samples = list(
        VirTyperRequest.objects.filter(project_name__pk=vir_typer_pk))
    pn = vir_typer_project.project_name
    vir_files = list()
    full_results = dict()
    full_results['data'] = list()
    codes = set()
    alleles = dict()
    outputs = list()
    samples = list()
    for sample in VirTyperRequest.objects.filter(
            project_name__pk=vir_typer_pk):
        samples.append(sample.sample_name)
    for sorted_sample in sorted(samples):
        for sample in VirTyperRequest.objects.filter(
                project_name__pk=vir_typer_pk):
            if sample.sample_name == sorted_sample:
                sequences = list()
                sample_dict = dict()
                sample_dict['sample_project'] = pn
                sample_dict['sample_name'] = sample.sample_name
                sample_dict['lsts'] = sample.LSTS_ID
                sample_dict['date_received'] = '{:%Y-%m-%d}'.format(
                    sample.date_received)
                sample_dict['isolate_source'] = sample.isolate_source
                sample_dict['organism'] = sample.putative_classification
                try:
                    codes.add(
                        (sample.putative_classification,
                         manager_codes[sample.putative_classification]['code'],
                         manager_codes[sample.putative_classification]
                         ['comments_en'], manager_codes[
                             sample.putative_classification]['comments_fr']))
                except KeyError:
                    pass
                sample_dict['identifier'] = list()
                sample_dict['allele'] = list()
                sample_dict['sequence'] = list()
                sample_dict['sequence_length'] = list()
                sample_dict['variable_locations'] = list()
                sample_dict['mean_quality'] = list()
                sample_dict['stdev_quality'] = list()
                alleles[sample.sample_name] = set()
                for vir_file in VirTyperFiles.objects.filter(
                        sample_name__pk=sample.pk):
                    vir_files.append(vir_file)
                    seq_identifier_well = str(
                        os.path.splitext(
                            vir_file.sequence_file)[0].split('_')[-2])
                    seq_identifier_num = os.path.splitext(
                        vir_file.sequence_file)[0].split('_')[-1]
                    seq_identifier_code = '_'.join(
                        (seq_identifier_well, seq_identifier_num))
                    sample_dict['identifier'].append(seq_identifier_code +
                                                     '\n')
                    result = VirTyperResults.objects.filter(
                        sequence_file__pk=vir_file.pk)
                    for vir_typer_result in result:
                        sample_dict['allele'].append(vir_typer_result.allele +
                                                     '\n')
                        alleles[sample.sample_name].add(
                            vir_typer_result.allele)
                        sequences.append({
                            sample.sample_name + '_' + seq_identifier_code:
                            vir_typer_result.trimmed_sequence
                        })
                        sample_dict['sequence_length'].append(
                            vir_typer_result.trimmed_sequence_len + '\n')
                        sample_dict['mean_quality'].append(
                            vir_typer_result.trimmed_quality_mean)
                        sample_dict['stdev_quality'].append(
                            vir_typer_result.trimmed_quality_stdev)

                consensus_sequence = sequence_consensus(
                    sequences, vir_typer_pk)
                for vir_file in VirTyperFiles.objects.filter(
                        sample_name__pk=sample.pk):
                    result = VirTyperResults.objects.filter(
                        sequence_file__pk=vir_file.pk)
                    for vir_typer_result in result:
                        html_string, variable_locations = sequence_html_string(
                            vir_typer_result.trimmed_sequence,
                            consensus_sequence)
                        sample_dict['variable_locations'].append(
                            variable_locations)
                        sample_dict['sequence'].append(html_string + '\n')
                full_results['data'].append(sample_dict)
                outputs.append(sample_dict)
    json_path = 'olc_webportalv2/static/ajax/vir_typer/{pk}/arrays.txt'.format(
        pk=vir_typer_pk)
    data_tables_path = '../../../../static/ajax/vir_typer/{pk}/arrays.txt'.format(
        pk=vir_typer_pk)
    os.makedirs(
        'olc_webportalv2/static/ajax/vir_typer/{pk}'.format(pk=vir_typer_pk),
        exist_ok=True)
    os.makedirs('olc_webportalv2/media/vir_typer/{pk}'.format(pk=vir_typer_pk),
                exist_ok=True)
    # Create the JSON-formatted output file
    with open(json_path, 'w') as json_out:
        json.dump(full_results, json_out)
    if request.method == 'POST':
        # Create a string of the HTML output using the appropriate template and variables
        html_string = render_to_string(
            'vir_typer/vir_typer_results_to_pdf.html', {
                'vir_typer_project': vir_typer_project,
                'date': '{:%Y-%m-%d}'.format(datetime.datetime.now()),
                'codes': sorted(list(codes)),
                'results': outputs,
                'vir_typer_samples': vir_typer_samples,
            })
        # Create an HTML object from the HTML string
        html = HTML(string=html_string, base_url=request.build_absolute_uri())
        # Set the links to the CSS files
        bootstrap_css = CSS(
            filename='olc_webportalv2/static/css/bootstrap.min.css')
        project_css = CSS(filename='olc_webportalv2/static/css/project.css')
        all_css = CSS(filename='olc_webportalv2/static/fonts/css/all.css')
        # Create a custom CSS string to make the page letter sized, with landscape orientation
        page_css = CSS(string='@page { size: Letter landscape; margin: 1cm }')
        #
        html.write_pdf(
            target=
            'olc_webportalv2/media/vir_typer/{pk}/VirusTyperReport_{pn}.pdf'.
            format(pk=vir_typer_pk, pn=vir_typer_project.project_name),
            stylesheets=[bootstrap_css, project_css, all_css, page_css],
            presentational_hints=True)
        # Download
        fs = FileSystemStorage(
            'olc_webportalv2/media/vir_typer/{pk}/'.format(pk=vir_typer_pk))
        with fs.open("VirusTyperReport_{pn}.pdf".format(
                pn=vir_typer_project.project_name)) as pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename="VirusTyperReport_{pn}.pdf"'\
                .format(pn=vir_typer_project.project_name)
            return response
    return render(
        request, 'vir_typer/vir_typer_results.html', {
            'vir_typer_project': vir_typer_project,
            'date': '{:%Y-%m-%d}'.format(datetime.datetime.now()),
            'codes': sorted(list(codes)),
            'json_results': data_tables_path,
            'results': outputs,
            'vir_typer_samples': vir_typer_samples,
            'vir_typer_files': vir_files,
        })
Exemplo n.º 2
0
from weasyprint import HTML

# Adapted from: https://github.com/Kozea/WeasyPrint/issues/23#issuecomment-312447974
# def generate_outline_str(bookmarks, indent=0):
#     outline_str = ""
#     for i, (label, (page, _, _), children) in enumerate(bookmarks, 1):
#         outline_str += ('%s%d. %s (page %d)' % (
#         ' ' * indent, i, label.lstrip('0123456789. '), page))
#         outline_str += generate_outline_str(children, indent + 2)
#         return outline_str

document = HTML('rules/RULES.html').render()

# table_of_contents_string = generate_outline_str(document.make_bookmark_tree())
# table_of_contents_document = HTML(string=table_of_contents_string).render()
# table_of_contents_page = table_of_contents_document.pages[0]
# document.pages.insert(0, table_of_contents_page)

document.write_pdf('_output/RULES.pdf')
def OdenPDF(request, id):
    from django.http import HttpResponse
    from django.template.loader import render_to_string
    from django.utils.text import slugify
    from django.contrib.auth.decorators import login_required
    from weasyprint import HTML
    from weasyprint.fonts import FontConfiguration
    from weasyprint import CSS
    compra = OrdenCabecera.objects.get(id=id)
    item = OrdenCompraBody.objects.filter(CompraHead=compra.id)
    prove = get_object_or_404(Proveedor, id=compra.Proveedor.id)
    produc = Producto.objects.filter(ProveedorItem=prove.id)
    sum = 0
    for a in item:
        total = a.Item.PrecioCompra * a.Cantidad
        sum = sum + total

    #response = HttpResponse(content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    response = HttpResponse(content_type="application/pdf")
    # response['Content-Disposition'] = "attachment;
    response[
        'Content-Disposition'] = "attachment; filename=ODE.{}.{}.{}.pdf".format(
            compra.FechaCreacion.year,
            str(compra.ContSerializeQtn).zfill(4),
            str(compra.SerializeQtn).zfill(2))
    font_config = FontConfiguration()
    html = render_to_string(
        "vil/tpl/ordendecompra.html", {
            'compra': compra,
            'Item': item,
            'request': request,
            'prove': prove,
            "subtotal": sum
        })
    css = CSS(string='''
	@page {
		@bottom-center {
			content: string(title);
			font-size:12px;
			border-top:1px solid black;
			color:#186a43;
			width:100%;
			height:50px;
		}
		}
	header {
		width: 0;
		height: 0;
		visibility: hidden;
		string-set: title content();
	}
	@font-face {
		font-family: "calibrilight";
	}
	body{
		font-family: 'calibrilight';
		font-size: 12px;
	}''',
              font_config=font_config)
    HTML(string=html).write_pdf(response,
                                stylesheets=[css],
                                font_config=font_config)
    return response
Exemplo n.º 4
0
def render_template(url):
    parts = [
        '''\
<!doctype html>
<meta charset=utf-8>
<title>WeasyPrint Navigator</title>
<style>
  form { position: fixed; z-index: 1; top: 8px; left: 16px; right: 0; }
  nav, input { font: 24px/30px sans-serif }
  input:not([type]) { background: rgba(255, 255, 255, .9); border-width: 2px;
                      border-radius: 6px; padding: 0 3px }
  input:not([type]):focus { outline: none }
  body { margin-top: 0; padding-top: 50px }
  section { box-shadow: 0 0 10px 2px #aaa; margin: 25px;
            position: relative; overflow: hidden; }
  section a { position: absolute; display: block }
  section a[href]:hover, a[href]:focus { outline: 1px dotted }
  nav { margin: 25px }
</style>
<body onload="var u=document.forms[0].url; u.value || u.focus()">
<form action="/" onsubmit="
  window.location.href = '/view/' + this.url.value; return false;">
<input name=url style="width: 80%" placeholder="Enter an URL to start"
  value="'''
    ]
    write = parts.append
    if url:
        html = HTML(url)
        url = html.base_url
        write(url)
    write('" />\n<input type=submit value=Go />\n')
    if url:
        write('<a href="/pdf/')
        write(url)
        write('">PDF</a>\n')
    write('</form>\n')
    if url:
        for width, height, data_url, links, anchors in get_pages(html):
            write('<section style="width: {0}px; height: {1}px">\n'
                  '  <img src="{2}">\n'.format(width, height, data_url))
            for link_type, target, (pos_x, pos_y, width, height) in links:
                href = ('#' + target if link_type == 'internal' else '/view/' +
                        target)
                write('  <a style="left: {0}px; top: {1}px; '
                      'width: {2}px; height: {3}px" href="{4}"></a>\n'.format(
                          pos_x, pos_y, width, height, href))
            for anchor_name, (pos_x, pos_y) in anchors.items():
                # Remove 60px to pos_y so that the real pos is below
                # the address bar.
                write(
                    '  <a style="left: {0}px; top: {1}px;" name="{2}"></a>\n'.
                    format(pos_x, pos_y - 60, anchor_name))
            write('</section>\n')
    else:
        write('''
<nav>
<h2>Examples:</h2>
<ul>
  <li><a href="/view/http://www.webstandards.org/files/acid2/test.html">
      Acid2</a></li>
  <li><a href="/view/http://www.w3.org/Style/CSS/current-work">
      CSS specifications</a></li>
  <li><a href="/view/http://en.wikipedia.org/">
      English Wikipedia</a></li>
</ul>
</nav>
''')
    return ''.join(parts).encode('utf8')
Exemplo n.º 5
0
def html_generator(manillas: list, template: str, eb: str):
    '''Generate the html of the qr and values list to further
    convert to pdf'''
    file_loader = FileSystemLoader('./')
    env = Environment(loader=file_loader)
    template = env.get_template(template)
    for manilla in manillas:
        manilla["qr"].save('output/qrs/' + str(manilla["data"]) + '.png')
    result = template.render(manillas=manillas, eb=eb)
    return result


# Ask for input
num_manillas = input("Número de Manillas: ")
eb = input("Estudio Bíblico: ")

output = eb.lower()
template = 'templates/template.html'
data = random_data(int(num_manillas), 5000000, 6000000)
qrs = qr_generator(data)
manillas = html_generator(qrs, template=template, eb=eb)

with open(output + '.html', 'w') as file:
    file.write(manillas)

with open(eb.lower() + '.txt', 'w') as file:
    for item in data:
        file.write(f"{item}\n")

HTML(filename=output + '.html').write_pdf(output + '.pdf')
Exemplo n.º 6
0
from django.test import TestCase
from weasyprint import HTML
from weasyprint.fonts import FontConfiguration
from jinja2 import Environment, FileSystemLoader

# load template
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('./templates/creator/certificate.html')

# render template
rendered = template.render(messages=['Hello','World'])

# write to PDF
HTML(string=rendered).write_pdf('weasyprint_jinja2_spike_output.pdf')


# Create your tests here.
Exemplo n.º 7
0
                 ydl.download([toDownload[i].url])
             print('youtube-dl supported!' + toDownload[i].id)
         except Exception as e:
             Unable.write(
                 str(e) + ' ' + toDownload[i].url +
                 ' http://www.reddit.com/' + toDownload[i].id +
                 '\n')
     else:
         print('ytdl exists! ' + toDownload[i].id)
     os.chdir(base)
 elif bool(toDownload[i].is_self):
     if not os.path.isfile(mypath + '/' + toDownload[i].id +
                           '.pdf'):
         if not bool(toDownload[i].over_18):
             HTML(toDownload[i].url.replace(
                 'www',
                 'old')).write_pdf(mypath + '/' +
                                   toDownload[i].id + '.pdf')
 elif bool(
         re.search('reddit\.com/gallery', toDownload[i].url,
                   re.IGNORECASE)):
     print('Reddit album!')
     response = requests.get(toDownload[i].url, headers=headers)
     soup = BeautifulSoup(response.text, 'lxml')
     img = soup.find_all('a', {'rel': 'noopener noreferrer'})
     if not (os.path.isdir(mypath + '/' + toDownload[i].id +
                           '/')):
         os.makedirs(mypath + '/' + toDownload[i].id + '/')
         for g, image in enumerate(img):
             download(
                 image['href'],
                 mypath + '/' + toDownload[i].id + '/' +
Exemplo n.º 8
0
from weasyprint import HTML
import logging

logging.basicConfig(level=logging.DEBUG)

URL = "http://tools.hde.nl/menc/site/guides/Pliss%C3%A9%20%26%20Duette%C2%AE%20Bottom-Up%20programming/"
OUT = "/home/admin-s/test.pdf"

for lp in range(0, 300):
    try:
        HTML(URL).write_pdf(OUT)
    except OSError as e:
        logging.exception(
            "**************** ERROR AT ATTEMPT: {} *******************".format(
                lp))
        break
Exemplo n.º 9
0
def get_pdf(filename):
    html_file = "./html/{}.html".format(filename)
    pdf_file = "./pdf/{}.pdf".format(filename)
    HTML(html_file).write_pdf(pdf_file)
    print("Done")
Exemplo n.º 10
0
def generate_report(url, username, password, component_key):
    server_version = get_string(url, username, password, "/api/server/version")

    json_data = get_json(
        url, username, password,
        "/api/navigation/component?componentKey=" + component_key)
    project_name = json_data["name"]
    project_organization = json_data["organization"]
    project_quality_gate = json_data["qualityGate"]
    project_quality_profiles = json_data["qualityProfiles"]
    project_analysis_date = dateutil.parser.parse(json_data["analysisDate"])
    project_analysis_date_utc = str(
        datetime.datetime.utcfromtimestamp(
            project_analysis_date.timestamp())) + " UTC"
    project_version = json_data["version"]

    quality_gates_table = ""
    if project_quality_gate:
        quality_gates_table += "<table><tr><th>Key</th><th>Name</th><th>Is Default</th></tr>"
        if 'key' in project_quality_gate:
            quality_gates_table += "<td>" + str(
                project_quality_gate['key']) + "</td>"
        else:
            quality_gates_table += "<td></td>"
        if 'name' in project_quality_gate:
            quality_gates_table += "<td>" + project_quality_gate[
                'name'] + "</td>"
        else:
            quality_gates_table += "<td></td>"
        if 'isDefault' in project_quality_gate:
            quality_gates_table += "<td>" + str(
                project_quality_gate['isDefault']) + "</td>"
        else:
            quality_gates_table += "<td></td>"

    quality_gates_table += "</table>"

    quality_profiles_table = ""
    if len(project_quality_profiles) > 0:
        quality_profiles_table += "<table><tr><th>Key</th><th>Name</th><th>Language</th></tr>"
        for quality_profile in project_quality_profiles:
            quality_profiles_table += "<tr>"
            if 'key' in quality_profile:
                quality_profiles_table += "<td>" + quality_profile[
                    'key'] + "</td>"
            else:
                quality_profiles_table += "<td></td>"
            if 'name' in quality_profile:
                quality_profiles_table += "<td>" + quality_profile[
                    'name'] + "</td>"
            else:
                quality_profiles_table += "<td></td>"
            if 'language' in quality_profile:
                quality_profiles_table += "<td>" + quality_profile[
                    'language'] + "</td>"
            else:
                quality_profiles_table += "<td></td>"
            quality_profiles_table += "</tr>"

    quality_profiles_table += "</table>"

    json_data = get_json(
        url, username, password,
        "/api/measures/component?additionalFields=metrics%2Cperiods&componentKey="
        + component_key +
        "&metricKeys=alert_status%2Cquality_gate_details%2Cbugs%2Cnew_bugs%2Creliability_rating%2Cnew_reliability_rating%2Cvulnerabilities%2Cnew_vulnerabilities%2Csecurity_rating%2Cnew_security_rating%2Ccode_smells%2Cnew_code_smells%2Csqale_rating%2Cnew_maintainability_rating%2Csqale_index%2Cnew_technical_debt%2Ccoverage%2Cnew_coverage%2Cnew_lines_to_cover%2Ctests%2Cduplicated_lines_density%2Cnew_duplicated_lines_density%2Cduplicated_blocks%2Cncloc%2Cncloc_language_distribution%2Cprojects%2Cnew_lines"
    )
    measures = json_data['component']['measures']
    periods = json_data["periods"]
    metrics = json_data["metrics"]

    quality_gate_status = get_value_for_metric_key("alert_status", measures)
    if quality_gate_status == "ERROR":
        quality_gate_status = "Failed"
    bugs = get_value_for_metric_key("bugs", measures)
    vulnerabilities = get_value_for_metric_key("vulnerabilities", measures)
    security_rating = get_value_for_metric_key("security_rating", measures)
    code_smells = get_value_for_metric_key("code_smells", measures)
    coverage = get_value_for_metric_key("coverage", measures)
    duplicated_blocks = get_value_for_metric_key("duplicated_blocks", measures)
    duplicated_lines_density = get_value_for_metric_key(
        "duplicated_lines_density", measures)
    ncloc = get_value_for_metric_key("ncloc", measures)
    ncloc_language_distribution = get_value_for_metric_key(
        "ncloc_language_distribution", measures)
    reliability_rating = get_value_for_metric_key("reliability_rating",
                                                  measures)
    sqale_index = get_value_for_metric_key("sqale_index", measures)
    sqale_rating = get_value_for_metric_key("sqale_rating", measures)

    period_index = 0
    period_details_table = ""

    if len(periods) > 0:
        period = periods[period_index]
        period_since = "previous version"
        if "parameter" in period:
            period_since = period["parameter"]
        else:
            if "mode" in period:
                period_since = period["mode"]
        period_started = period["date"]
        new_bugs = get_value_for_metric_key_and_period_index(
            "new_bugs", measures, period_index)
        new_vulnerabilities = get_value_for_metric_key_and_period_index(
            "new_vulnerabilities", measures, period_index)
        new_security_rating = get_value_for_metric_key_and_period_index(
            "new_security_rating", measures, period_index)
        new_code_smells = get_value_for_metric_key_and_period_index(
            "new_code_smells", measures, period_index)
        new_lines = get_value_for_metric_key_and_period_index(
            "new_lines", measures, period_index)
        new_lines_to_cover = get_value_for_metric_key_and_period_index(
            "new_lines_to_cover", measures, period_index)
        new_reliability_rating = get_value_for_metric_key_and_period_index(
            "new_reliability_rating", measures, period_index)
        new_technical_debt = get_value_for_metric_key_and_period_index(
            "new_technical_debt", measures, period_index)
        new_maintainability_rating = get_value_for_metric_key_and_period_index(
            "new_maintainability_rating", measures, period_index)
        period_details_table += "<h3>Leak Period: since " + period_since + ", started " + period_started + "</h3>"
        period_details_table += "<table><tr><th>Issue Type</th><th>Value</th></tr>"
        period_details_table += "<tr><td>New Bugs</td><td>" + new_bugs + "</td></tr>"
        period_details_table += "<tr><td>New Vulnerabilities</td><td>" + new_vulnerabilities + "</td></tr>"
        period_details_table += "<tr><td>Security Rating on New Code</td><td>" + new_security_rating + "</td></tr>"
        period_details_table += "<tr><td>New Code Smells</td><td>" + new_code_smells + "</td></tr>"
        period_details_table += "<tr><td>New Lines</td><td>" + new_lines + "</td></tr>"
        period_details_table += "<tr><td>Lines to Cover on New Code</td><td>" + new_lines_to_cover + "</td></tr>"
        period_details_table += "<tr><td>Reliability Rating on New Code</td><td>" + new_reliability_rating + \
                                "</td></tr>"
        period_details_table += "<tr><td>Added Technical Debt</td><td>" + new_technical_debt + "</td></tr>"
        period_details_table += "<tr><td>Maintainability Rating on New Code</td><td>" + new_maintainability_rating + \
                                "</td></tr>"
        period_details_table += "</table><br>"

    quality_gate_details = get_value_for_metric_key("quality_gate_details",
                                                    measures)
    quality_gate_details_table = ""
    if quality_gate_details:
        conditions = json.loads(quality_gate_details)["conditions"]
        if len(conditions) > 0:
            quality_gate_details_table += "<table><tr><th>Metric</th><th>Actual Value</th><th>Operand</th>" \
                                          "<th>Expected Value</th></tr>"
            for condition in conditions:
                quality_gate_details_table += "<tr>"
                if 'level' in condition and condition['level'] == "ERROR":
                    if 'metric' in condition:
                        metric_name = get_name_for_metric_key(
                            condition['metric'], metrics)
                        quality_gate_details_table += "<td>" + metric_name + "</td>"
                    else:
                        quality_gate_details_table += "<td></td>"
                    if 'actual' in condition:
                        quality_gate_details_table += "<td>" + condition[
                            'actual'] + "</td>"
                    else:
                        quality_gate_details_table += "<td></td>"
                    if 'op' in condition:
                        quality_gate_details_table += "<td>" + condition[
                            'op'] + "</td>"
                    else:
                        quality_gate_details_table += "<td></td>"
                    if 'error' in condition:
                        quality_gate_details_table += "<td>" + condition[
                            'error'] + "</td>"
                    else:
                        quality_gate_details_table += "<td></td>"

            quality_gate_details_table += "</tr></table>"

    json_data = get_json(
        url, username, password, "/api/issues/search?componentKeys=" +
        component_key + "&statuses=OPEN&ps=1")
    if json_data['total'] == 0:
        print("no data returned - no report will be generated")
    else:
        print("found " + str(json_data['total']) +
              " issues. Report will be generated...")
        json_all = "["

        # GET ALL ISSUES (max. 500) OF TYPE VULNERABILITY
        json_vulnerabilities = get_json(
            url, username, password, "/api/issues/search?componentKeys=" +
            component_key + "&statuses=OPEN&ps=500&types=VULNERABILITY")
        if json_vulnerabilities['total'] > 0:
            print("found " + str(json_vulnerabilities['total']) +
                  " issues of type VULNERABILITY")
            json_vulnerabilities = filter_json(json_vulnerabilities)
            json_all += json_vulnerabilities

        # GET ALL ISSUS (max. 500) OF TYPE BUG
        json_bugs = get_json(
            url, username, password, "/api/issues/search?componentKeys=" +
            component_key + "&statuses=OPEN&ps=500&types=BUG")
        if json_bugs['total'] > 0:
            print("found " + str(json_bugs['total']) + " issues of type BUG")
            json_bugs = filter_json(json_bugs)
            if json_all != "[":
                json_all += ","
            json_all += json_bugs

        # GET ALL ISSUES (max. 500) OF TYPE CODE_SMELL
        json_codesmells = get_json(
            url, username, password, "/api/issues/search?componentKeys=" +
            component_key + "&statuses=OPEN&ps=500&types=CODE_SMELL")
        if json_codesmells['total'] > 0:
            print("found " + str(json_codesmells['total']) +
                  " issues of type CODE_SMELL")
            json_codesmells = filter_json(json_codesmells)
            if json_all != '[':
                json_all += ","
            json_all += json_codesmells

        json_all += "]"

        # GENERATE PDF
        json_data = json.loads(json_all)
        html_issues = ""

        for i in json_data:
            html_issues += "<tr>"
            if 'type' in i:
                html_issues += "<td>" + i['type'] + "</td>"
            else:
                html_issues += "<td></td>"
            if 'component' in i:
                component = i['component']
                component = component.split(":")[-1]
                html_issues += "<td>" + component + "</td>"
            else:
                html_issues += "<td></td>"
            if 'startLine' in i:
                html_issues += "<td>" + str(i['startLine']) + "</td>"
            else:
                html_issues += "<td></td>"
            if 'endLine' in i:
                html_issues += "<td>" + str(i['endLine']) + "</td>"
            else:
                html_issues += "<td></td>"
            if 'message' in i:
                html_issues += "<td>" + i['message'] + "</td>"
            else:
                html_issues += "<td></td>"
            html_issues += "</tr>"

        now_in_utc = datetime.datetime.utcfromtimestamp(
            datetime.datetime.now().timestamp())
        creation_date = str(now_in_utc) + " UTC"

        env = Environment(loader=FileSystemLoader('.'))
        template = env.get_template(TEMPLATE)
        template_vars = {
            "project": component_key,
            "server_version": server_version,
            "creation_date": creation_date,
            "quality_gate_status": quality_gate_status,
            "bugs": bugs,
            "vulnerabilities": vulnerabilities,
            "security_rating": security_rating,
            "code_smells": code_smells,
            "coverage": coverage,
            "duplicated_blocks": duplicated_blocks,
            "duplicated_lines_density": duplicated_lines_density,
            "ncloc": ncloc,
            "ncloc_language_distribution": ncloc_language_distribution,
            "reliability_rating": reliability_rating,
            "sqale_index": sqale_index,
            "sqale_rating": sqale_rating,
            "issue_table": html_issues,
            "quality_gate_details_table": quality_gate_details_table,
            "project_name": project_name,
            "project_organization": project_organization,
            "project_version": project_version,
            "quality_gates_table": quality_gates_table,
            "quality_profiles_table": quality_profiles_table,
            "project_analysis_date": project_analysis_date_utc
        }
        html_out = template.render(template_vars)
        report_name = "sonarqube_report_" + project_name + "_" + now_in_utc.strftime(
            "%Y%m%d%H%M%S") + ".pdf"
        HTML(string=html_out,
             base_url=".").write_pdf(report_name, stylesheets=["style.css"])
Exemplo n.º 11
0
def render(cat_image, background_image, text):
    ugly_text = uglify(text)
    font = random.choice(fonts)
    color = random.choice(colors)
    pixabay_logo = os.path.join(os.getcwd(), 'spreukbot/pixabay-logo.png')
    cat_url, cat_width, cat_height = cat_image
    bg_url, bg_width, bg_height = background_image
    demo_page = f'''
        data:text/html,
        <!doctype html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>LOL</title>
            <link href="https://fonts.googleapis.com/css?family=Pacifico|Baloo+Tamma|Merriweather|Poiret+One" rel="stylesheet">
            <style>
                @page {{
                    margin: 0;
                    size: {cat_width * 3}px {cat_height}px;
                }}
                * {{
                    padding: 0;
                    margin: 0;
                    text-shadow: #FC0 1px 0 10px;
                }}
                html, body {{
                    text-align: center;
                    padding: 0;
                }}
                body:before {{
                    content: "";
                    position: fixed;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    background-size: cover;
                    background-image: url('{bg_url.replace('https://pixabay.com', 'http://spreukbot-pixabay.barkr.uk')}');
                    background-repeat: no-repeat;
                }}
                p {{
                    position: absolute;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    font-family: {font};
                    font-size: 32px;
                    color: {color};
                    text-stroke: 2px white;
                }}
                p.shadow {{
                    color: black !important;
                    top: 1px;
                    left: 1px;
                }}
                div.watermark {{
                    position: absolute;
                    right: 10%;
                    bottom: 10%;
                    transform: rotate(-45deg);
                    opacity: .2;
                    width: 20%;
                }}
                img.pixabay {{
                    position: absolute;
                    right: 0;
                    top: 0;
                }}
            </style>
        </head>
        <body>
        <p>text</p>
        <div class="watermark">
            Wijze spreuken om te delen van Marko V. Keten
        </div
        <img class="pixabay" src="cat_url">
        </body>
        </html>
    '''
    from weasyprint import HTML
    from weasyprint.logger import LOGGER as weasyprint_logger
    import logging
    logging.getLogger('weasyprint').setLevel(logging.DEBUG)
    weasyprint_logger.addFilter(WeasyprintLoggerFilter())
    weasyprint_logger.setLevel(logging.DEBUG)
    data = HTML(string=demo_page).write_png()
    return data
Exemplo n.º 12
0
def document_png():
    # We didn’t bother to make a ``render_png`` helper
    # but of course you can still use WeasyPrint’s PNG output.
    return Response(HTML('/').write_png(), mimetype='image/png')
Exemplo n.º 13
0
    def generar_pdf(self, request, orden_examen):
        if not orden_examen.especial:
            multifirma = OrdenExamen.objects.select_related(
                'examen', 'examen__subgrupo_cups').prefetch_related(
                    'mis_firmas',
                    'mis_firmas__especialista',
                    'mis_firmas__especialista__especialidad',
                ).annotate(can_firmas=Count("mis_firmas")).filter(
                    pk=orden_examen.id,
                    can_firmas__gt=1,
                    examen__especial=False,
                    examen_estado=2,
                )

            una_firma = OrdenExamenFirmas.objects.select_related(
                'especialista',
                'especialista__especialidad',
                'orden_examen',
                'orden_examen__examen',
                'orden_examen__examen__subgrupo_cups',
            ).annotate(especialist=F('especialista'),
                       can_firmas=Count("orden_examen__mis_firmas")).filter(
                           orden_examen=orden_examen,
                           orden_examen__examen__especial=False,
                           can_firmas=1,
                           orden_examen__examen_estado=2,
                       )

            ctx = {
                'paciente': orden_examen.orden.paciente,
                'orden': orden_examen.orden,
                'entidad': orden_examen.orden.entidad,
                'medico_remitente': orden_examen.orden.medico_remitente,
                'multifirma': multifirma,
                'una_firma': una_firma,
                'examen': orden_examen,
            }
            html_get_template = get_template(
                'email/ordenes/resultados/datos_orden.html').render(ctx)
            html = HTML(string=html_get_template,
                        base_url=request.build_absolute_uri())
            header_datos = html.render(stylesheets=[
                CSS(string='div {position: fixed; top: 0, margin:0, padding:0}'
                    )
            ])

            header_datos_page = header_datos.pages[0]
            header_datos_body = get_page_body(
                header_datos_page._page_box.all_children())
            header_datos_body = header_datos_body.copy_with_children(
                header_datos_body.all_children())

            html_get_template = get_template(
                'email/ordenes/resultados/resultados.html').render(ctx)

            html = HTML(string=html_get_template,
                        base_url=request.build_absolute_uri())

            main_doc = html.render(
                stylesheets=[CSS('static/css/pdf_ordenes_resultado.min.css')])
            for i, page in enumerate(main_doc.pages):
                page_body = get_page_body(page._page_box.all_children())
                page_body.children += header_datos_body.all_children()

            output = BytesIO()
            main_doc.write_pdf(target=output)
            orden_examen.pdf_examen.delete()
            filename = "%s_ALE%s.pdf" % (orden_examen.nro_examen,
                                         random.randint(1000, 9999))
            orden_examen.pdf_examen.save(filename, File(output))
            output.close()
Exemplo n.º 14
0
from utils import *
import pandas as pd
import numpy as np
import pprint
sched = get_schedule()
extra_key = str(max([int(x) for x in sched.keys()]))
del sched[extra_key]

html_str = ''
for i in sched:
    df = pd.DataFrame.from_dict(sched[i]).transpose()
    df.index.name = "Match-ID"
    df.columns = ['Player 1', 'Player 2']
    html_str += "<h2>Day {}</h2>".format(i) + "\n" + df.to_html() + "\n\n"

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("matches.html")
template_vars = {"title": "Matchups", "national_pivot_table": html_str}
html_out = template.render(template_vars)
from weasyprint import HTML
HTML(string=html_out).write_pdf("matchups.pdf", stylesheets=["style.css"])
Exemplo n.º 15
0
def _create_pdf(rendered_template, absolute_url):
    from weasyprint import HTML

    pdf_file = HTML(string=rendered_template,
                    base_url=absolute_url).write_pdf()
    return pdf_file
Exemplo n.º 16
0
    <meta charset="utf-8">
</head>
<body>
'''
for k in all_html:
    html += k + '<br>' + all_html[k] + '<hr>'

html += "</body></html>"

font_config = FontConfiguration()
css = CSS(string='''
    * {
        font-size : 0.8rem;
    }
    body {
        backgroud: black;
    }
    @font-face {
        font-family: Gentium;
        src: url(http://example.com/fonts/Gentium.otf);
    }
    h1 { font-family: Gentium }
    img {width: 30; height: 60}''',
          font_config=font_config)

#print(html)
report_html = HTML(string=html)
report_html.write_pdf(target='test.pdf',
                      stylesheets=[css],
                      font_config=font_config)
Exemplo n.º 17
0
def export_pdf(request):

    todays_date = datetime.date.today()
    year = todays_date.strftime("%d-%b-%Y").split("-")[2]
    month = todays_date.strftime("%d-%b-%Y").split("-")[1]
    year_list = {
        "Jan": 1,
        "Feb": 2,
        "Mar": 3,
        "Apr": 4,
        "May": 5,
        "Jun": 6,
        "Jul": 7,
        "Aug": 8,
        "Sep": 9,
        "Oct": 10,
        "Nov": 11,
        "Dec": 12
    }
    month = year_list[month]
    allexpense = 0

    date_start_month = datetime.date(int(year), month, 1)
    expenses = Expense.objects.filter(date__gte=date_start_month,
                                      date__lte=todays_date)

    if request.method == 'POST':
        data = request.body
        array1 = data.split(b'&')
        startAr = array1[0].decode("utf-8")
        endAr = array1[1].decode("utf-8")
        startAr = startAr.split('=')
        endAr = endAr.split('=')

        start = startAr[1]
        end = endAr[1]
        yearStart = start.split('-')[0]
        monthStart = start.split('-')[1]
        dayStart = start.split('-')[2]

        yearEnd = end.split('-')[0]
        monthEnd = end.split('-')[1]
        dayEnd = end.split('-')[2]

        start = datetime.date(int(yearStart), int(monthStart), int(dayStart))
        end = datetime.date(int(yearEnd), int(monthEnd), int(dayEnd))

        if end and start:
            expenses = Expense.objects.filter(date__gte=start, date__lte=end)

    response = HttpResponse(content_type='text/pdf')
    response['Content-Disposition'] = 'attachement; filename=BadolExpenses' + \
        str(datetime.datetime.now()) + '.pdf'

    response['Content-Transfer-Encoding'] = 'binary'
    sum = expenses.aggregate(Sum('amount'))

    try:
        currency = UserPreference.objects.get(user=request.user).currency
    except:
        messages.success(request, 'Veuillez configurer votre monnaie')
        return redirect('preferences')

    allexpense = "{:.2f}".format(sum['amount__sum'])
    allexpense = str(allexpense) + currency

    html_string = render_to_string('expenses/pdf-output.html', {
        'expenses': expenses,
        'total': allexpense
    })
    html = HTML(string=html_string)
    result = html.write_pdf()

    with tempfile.NamedTemporaryFile(delete=True) as output:
        output.write(result)
        output.flush()

        output = open(output.name, 'rb')
        response.write(output.read())

    return response
Exemplo n.º 18
0
def printreport(request, permit_request):
    """Return a PDF of the permit request generated using weasyprint.

    Parameters
    ----------
    request : <class 'django.core.handlers.wsgi.WSGIRequest'>
        The user request.
    permit_request : <class 'permits.models.PermitRequest'>
        The permit request.

    Returns
    -------
    pdf_permit : <class 'bytes'>
        The PDF of the permit request.
    """

    geo_times = permit_request.geo_time.all()
    printurl = get_map_url(geo_times, permit_request.pk)
    print_date = timezone.now()
    validations = permit_request.validations.all()
    objects_infos = services.get_permit_objects(permit_request)
    actor_types = dict(models.ACTOR_TYPE_CHOICES)

    contacts = [(actor_types.get(contact['actor_type'].value(), ''), [
        (field.label, field.value()) for field in contact
        if field.name not in {'id', 'actor_type'}
    ]) for contact in services.get_permitactorformset_initiated(permit_request)
                if contact['id'].value()]

    author = permit_request.author
    administrative_entity = permit_request.administrative_entity

    html = render(
        request, "permits/print/printpermit.html", {
            'permit_request':
            permit_request,
            'contacts':
            contacts,
            'author':
            author,
            'objects_infos':
            objects_infos,
            'print_date':
            print_date,
            'administrative_entity':
            administrative_entity,
            'geo_times':
            geo_times,
            'map_image':
            printurl,
            'validations':
            validations,
            'logo_main':
            b64encode(
                administrative_entity.logo_main.open().read()).decode("utf-8")
            if administrative_entity.logo_main else '',
            'logo_secondary':
            b64encode(administrative_entity.logo_secondary.open().read(
            )).decode("utf-8") if administrative_entity.logo_secondary else '',
            'image_signature_1':
            b64encode(administrative_entity.image_signature_1.open().read()).
            decode("utf-8") if administrative_entity.image_signature_1 else '',
            'image_signature_2':
            b64encode(administrative_entity.image_signature_2.open().read()).
            decode("utf-8") if administrative_entity.image_signature_2 else '',
        })

    pdf_permit = HTML(
        string=html.content, base_url=request.build_absolute_uri()).write_pdf(
            stylesheets=[CSS('/code/static/css/printpermit.css')])

    file_name = 'permis_' + str(permit_request.pk) + '.pdf'
    permit_request.printed_file.save(file_name, ContentFile(pdf_permit), True)
    permit_request.printed_at = timezone.now()
    permit_request.printed_by = request.user.get_full_name()
    permit_request.save()

    return pdf_permit
Exemplo n.º 19
0
import os
from weasyprint import HTML
from flask_frozen import Freezer
from flask_weasyprint.test_app import app

ROOT = os.path.dirname(os.path.abspath(__file__))
os.chdir(ROOT)

app.config['FREEZER_DESTINATION'] = os.path.join(ROOT, 'frozen_demo')
app.config['FREEZER_RELATIVE_URLS'] = True
Freezer(app).freeze()

HTML('http://www.pycon.fr/2012/schedule/').write_pdf(
    'PyConFR_2012_schedule.pdf', stylesheets=['print.css'])
Exemplo n.º 20
0
def quote(req, num=None, pdf=False):
    user = req.user
    quote_breadcrumb = Breadcrumb('Quotes', 'eRacks Quote List for %s' % user,
                                  '/quotes/')

    if 0:  # messages_test
        from django.contrib import messages
        #messages.add_message(req, messages.INFO, 'Hello world.')

        #Some shortcut methods provide a standard way to add messages with commonly used tags (which are usually represented as HTML classes for the message):

        messages.debug(req, '%s SQL statements were executed.' % 123)
        messages.info(req, '%s SQL statements were executed.' % 123)
        messages.info(req, 'Three credits remain in your account.')
        messages.success(req, 'Profile details updated.')
        messages.warning(req, 'Your account expires in three days.')
        messages.error(req, 'Document deleted.')

    if num:
        # render it directly

        try:
            q = Quote.objects.get(quote_number=num)
        except Quote.DoesNotExist:
            raise Http404

        if q:
            address1 = Address.objects.filter(customer=q.customer)
        #print address1
        shipping_addr = None
        billing_addr = None
        if address1:
            for address in address1:
                print address.type
                if address.type == "shipping":
                    shipping_addr = address
                if address.type == "billing":
                    billing_addr = address
                if address.type == "both":
                    shipping_addr = address
                    billing_addr = address

        prod = dict(
            address1=address1,
            shipping_addr=shipping_addr,
            billing_addr=billing_addr,
            q=q,
            totprice=q.totprice,
            summary=q.summary,
            notes=q.header_items,  # terms, discounts, etc?
            #qty        = q.totqty,
            qty=1,
            opts={},
            baseprice=q.totprice,
            weight=q.shipping,
            sku='Quote/%s' % q.quote_number,
            order_total=q.totprice + q.shipping)

        if req.POST.get('add', None):  # add quote to cart as product
            prod = dict(
                totprice=q.totprice,
                summary=q.summary,
                notes=q.header_items,  # terms, discounts, etc?
                #qty        =   q.totqty,
                qty=1,
                opts={},
                baseprice=q.totprice,
                weight=q.shipping,
                sku='Quote/%s' % q.quote_number,
                order_total=q.totprice + q.shipping)
            ses = req.session
            ses['cart'] = ses.get('cart', []) + [prod]
            ses['prod'] = {}

            return HttpResponseRedirect('/cart/')

        text = "Your email confirmation, as PDF attachment\n\n"
        html_string = render_to_string('pdf/index.html', context=prod)
        html = HTML(string=html_string, base_url=req.build_absolute_uri())
        pdf = html.write_pdf()
        #return HttpResponse (pdf, content_type='application/pdf')

        msg = EmailMultiAlternatives(
            'Your %s eracks quote #%s' % (settings.HNN[0], q.quote_number),
            text,  # nope: '',  # let's try attaching the text,
            settings.ORDER_FROM_EMAIL,
            ['*****@*****.**'])
        msg.attach('eRacks_Quote_#%s.pdf' % q.quote_number, pdf,
                   "application/pdf")
        # msg.send()

        if req.POST.get('pdf', None):  # Render PDF file & return to user
            response = HttpResponse(content_type='application/pdf')
            response[
                'Content-Disposition'] = 'attachment; filename=eRacks_Quote_#%s.pdf' % q.quote_number
            response.write(pdf)
            return response

        # Drop thru: email admin, render the quote_number

        if not user.is_staff:
            subject = "%s user accessed %s" % (settings.HOST_NAME, req.path)
            ip = req.META.get(
                'HTTP_X_FORWARDED_FOR',
                req.META.get('HTTP_X_REAL_IP',
                             req.META.get('REMOTE_ADDR', 'unknown')))
            message = quote_viewed_template.format(s=settings,
                                                   ip=ip,
                                                   u=user,
                                                   r=req)
            to = settings.CONTACT_EMAILS
            fm = '*****@*****.**'  # user.email
            send_mail(subject,
                      message,
                      fm,
                      to,
                      fail_silently=not settings.DEBUG)

        for line in q.quotelineitem_set.all(
        ):  # lame: dj templates can't do multiplcation or expressions
            line.ext = line.quantity * line.price

        return render_to_response(
            'quote.html',
            dict(title="Quote: %s" % q.quote_number,
                 q=q,
                 breadcrumbs=(quote_breadcrumb,
                              Breadcrumb('Quote %s' % q.quote_number,
                                         'Quote %s' % q.quote_number,
                                         '/quotes/%s' % q.quote_number))),
            context_instance=RequestContext(req))
    else:
        # render the list
        if user.is_staff:
            qs = Quote.objects.filter(approved_by=user)
        else:
            qs = Quote.objects.filter(customer__user=user)

        return render_to_response('entries.html',
                                  dict(
                                      entries=qs,
                                      title='Quote list for %s' % user,
                                      breadcrumbs=(quote_breadcrumb, ),
                                  ),
                                  context_instance=RequestContext(req))
Exemplo n.º 21
0
    def post(self):
        data = json.loads(request.data)

        # drive variant
        drive = [data['product'], data['variant']]

        # default values and add offsets if not None
        HOR_F1200_MK = 422. + float(
            data['xshift'] if data['xshift'] != None else 0)
        VER_F1200_MK = 170. + float(
            data['yshift'] if data['yshift'] != None else 0)

        # basic components
        measure_rh_a = (float(data['width']) / 2) - 123.
        measure_rh_b = float(data['width']) - 307.
        measure_rv_a = (float(data['height']) / 2) - 225.
        measure_rv_b = float(data['height']) - 409.
        measure_rv_mk = VER_F1200_MK - 122.

        # optional component horizontal
        measure_rh_no_MV = HOR_F1200_MK - 175.
        # optional component horizontal and MV
        measure_rh_MV_a = HOR_F1200_MK - 225.
        measure_rh_MV_b = (float(data['width']) / 2) - 74.

        measures = []

        # add basic rods
        measures = measures + [
            measure_rh_a, measure_rh_b, measure_rv_a, measure_rv_b,
            measure_rv_mk
        ]

        # select optional rods
        if data['variant'] != "vertical" and int(data['height']) >= 1250:
            measures.append(measure_rh_MV_a)
            measures.append(measure_rh_MV_b)
        elif data['variant'] != "vertical":
            measures.append(measure_rh_no_MV)

        # render html file to python string
        render = render_template('pdf.html',
                                 measurements=measures,
                                 drive=drive)

        # define html to be converted to pdf
        html = HTML(string=render)

        # define path to css file for pdf styling
        css = CSS(filename='/app/app/templates/style.css')

        # generate pdf from html with css
        pdf = html.write_pdf(stylesheets=[css])

        # build the http response to request
        response = make_response(pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers[
            'Content-Disposition'] = 'attachment;filename=output.pdf'

        return response
Exemplo n.º 22
0
def html2pdf(html, outputfile, stylesheets=None):

    document = HTML(string=html)

    with open(outputfile, 'wb') as f:
        document.write_pdf(f, stylesheets=stylesheets)
import argparse

parser = argparse.ArgumentParser(description='PDF maker with custom arguments')
parser.add_argument("--title", default="", help="This is the title of the PDF")
parser.add_argument("--subtitle",
                    default="",
                    help="This is the subtitle of the PDF")
parser.add_argument("--text", default="", help="This is the text of the PDF")
parser.add_argument("--outputpath", default="", help="This is the output path")
args = parser.parse_args()
title = args.title
subtitle = args.subtitle
text = args.text
outputpath = args.outputpath
if not outputpath:
    outputpath = 'pdfs/5_test_complicated_template_with_arguments.pdf'

root = os.path.dirname(os.path.abspath(__file__))
htmlpath = os.path.join(root, 'templates',
                        '5_test_complicated_template_with_arguments.html')
with open(htmlpath, 'r') as file:
    html = file.read()

    html = html.replace("{% title %}", title)
    html = html.replace("{% subtitle %}", subtitle)
    html = html.replace("{% text %}", text)

    # now add the substitutions

    pdf_file = HTML(string=html).write_pdf(outputpath)
Exemplo n.º 24
0
import markdown as md
import codecs
from weasyprint import HTML


def htmlHead(str, styleTag=False):
    if styleTag:
        css_input = 'generic css style'
        outStr = '<html>\n\t<head>\n\t\t<style>' + css_input + '</style>\n\t</head>\n\t<body>' + str + '</body>\n<html>'
    else:
        outStr = '<html>\n\t<body>' + str + '</body>\n<html>'
    return outStr


mdStr = htmlHead('test str here')
fileName = "test.html"
output_file = codecs.open(fileName,
                          "w",
                          encoding="utf-8",
                          errors="xmlcharrefreplace")
output_file.write(mdStr)
HTML(fileName).write_pdf('test.pdf')
Exemplo n.º 25
0
 def download_file(self, html, file_name):
     response = HttpResponse(content_type='application/pdf')
     response['Content-Disposition'] = 'attachment; filename={}.pdf'.format(
         file_name)
     HTML(string=html).write_pdf(response)
     return response
Exemplo n.º 26
0
def html_to_pdf(html: str, template_name: str):
    html = HTML(string=html)
    css = CSS(filename=get_css_location(template_name),
              font_config=font_config)
    return html.write_pdf(stylesheets=[css], font_config=font_config)
Exemplo n.º 27
0
  html = commonmark.commonmark(text)

  css = ""
  with open('./style.css', encoding='utf-8') as fr:
    css = fr.read()
  html_full = "<!DOCTYPE html>\n<html>\n<head>\n<style>\n"
  html_full += '@page { size: A4; margin: 1cm }\n'
  html_full += css
  if args.dc:
    with open('./style_doublecolumn.css', encoding='utf-8') as fr:
      css = fr.read()
    html_full += css
  html_full += "</style>\n</head>\n<body>\n"
  html_full += html
  html_full += "</body>\n</html>"

  html = HTML(string=html_full, encoding='utf-8')

  if args.output == None:
    out_file = os.path.splitext(args.input)[0] + '.pdf'
    html_file = os.path.splitext(args.input)[0] + '.html'
  else:
    out_file = os.path.splitext(args.output)[0] + '.pdf'
    html_file = os.path.splitext(args.output)[0] + '.html'

  if args.html:
    with open(html_file, "w", encoding='utf-8') as fw:
      fw.write(html_full)

  html.write_pdf(out_file)
Exemplo n.º 28
0
    def postpub(self):
        """
        main publication function
        """
        res = []
        pivot = self.pivot
        body = pivot.xpath('/*/*[local-name() = "body"]')[0]
        head = pivot.xpath('/*/*[local-name() = "head"]')[0]

        # add css
        css = self.scriptdef.xpath('string(parameters/parameter[@name="CSS"]/@value)')
        logger.debug(css)
        baseurl = "file://%s"%(self.getOsPath("/"))
        csslink = "%s/kolekti/publication-templates/weasyprint/css/%s.css"%(baseurl,css)
 
        logger.debug(csslink)
        ET.SubElement(head,'link',attrib = {
            "rel":"stylesheet",
            "type":"text/css",
            "href": csslink
            })
        ET.SubElement(head,'base',attrib = {
            "href": baseurl
            })
        
       
        # make image urls relative in pivot
        for media in pivot.xpath("//h:img[@src]|//h:embed[@src]", namespaces=self.nsmap):
            src = media.get('src')
            if src[0] == "/":
                media.set('src', src[1:])


        # produce pdf once
        pubdir = self.pubdir(self.assembly_dir, self.profile)        
        pdf = os.path.join(self.getOsPath(pubdir),self.publication_file+'.pdf')
        
        HTML(string = ET.tostring(pivot)).write_pdf(pdf)

        if self.scriptdef.xpath('boolean(parameters/parameter[@name="two_passes"]/@value = "yes")'):
            # count pages in pdf
            with open(pdf, 'rb') as pdffile:
                nbpages = PdfFileReader(pdffile).getNumPages() 
                
            logger.debug( "pdf nbpages : %d"%(nbpages,))
    
            # update pivot body attributes
            body.set('data-pagecount', str(nbpages))
            body.set('data-pagecount-mod-2', str(nbpages % 2))
            body.set('data-pagecount-mod-4', str(nbpages % 4))
            body.set('data-pagecount-mod-8', str(nbpages % 8))
            body.set('data-pagecount-mod-16', str(nbpages % 16))


            pivfile = pubdir + "/document_nbpage_attrs.xhtml"
            self.xwrite(pivot, pivfile, sync = False)
            
            # produce pdf with modified pivot
            HTML(string = ET.tostring(pivot)).write_pdf(pdf)

        subst = {}
        for p in self.scriptdef.xpath('parameters/parameter'):
            subst.update({p.get('name'):p.get('value')})
                
        pubdir = self.pubdir(self.assembly_dir, self.profile)
        subst.update({
            "APPDIR":self._appdir,
            "PLUGINDIR":self._plugindir,
            "PUBDIR":self.getOsPath(pubdir),
            "SRCDIR":self.getOsPath(self.assembly_dir),
            "BASEURI":self.getUrlPath(self.assembly_dir) + '/',
            "PUBURI":pubdir,
            "PUBNAME":self.publication_file,
        })
        xl=self.scriptspec.find('link')
        outfile=self._substscript(xl.get('name'), subst, self.profile)
        outref=self._substscript(xl.get('ref'), subst, self.profile)
        outtype=xl.get('type')

        res=[{"type":outtype, "label":outfile, "url":outref, "file":outref}]
        return res
Exemplo n.º 29
0
def export_report(request, pk):
    """
    Export the reach back report to the user upon request
    Parameters
    ----------
    request: rest_framework.request.Request
        The request object
    pk: int
        Flowcell primary key

    Returns
    -------
    A PDF blob to be downloaded
    """
    flowcell = Flowcell.objects.get(pk=pk)
    task = JobMaster.objects.filter(flowcell_id=pk, job_type_id=10).last()
    run = flowcell.runs.first()
    metagenomics_run = True if task else False
    alignment_data = PafSummaryCov.objects.filter(
        job_master__flowcell_id=pk).exclude(
            job_master__job_type__id=16).values()
    report_info = {
        "run_id":
        run.name,
        "read_count":
        run.summary.read_count,
        "yield":
        run.summary.total_read_length,
        "avg_read_length":
        run.summary.avg_read_length,
        "run_date":
        run.summary.first_read_start_time.strftime("%d/%m/%Y"),
        "run_duration":
        str(run.summary.last_read_start_time -
            run.summary.first_read_start_time),
        "alignment_data":
        bool(alignment_data),
        "metagenomics_data":
        metagenomics_run
    }
    if metagenomics_run:
        metadata = Metadata.objects.get(task=task)
        meta_dict = {
            "unclassified":
            metadata.unclassified,
            "classified":
            metadata.classified,
            "percent_classified":
            round(metadata.classified / run.summary.read_count * 100, 2),
            "percent_unclassified":
            round(metadata.unclassified / run.summary.read_count * 100, 2),
        }
        report_info.update(meta_dict)
    if alignment_data:
        alignment_dict = {"alignment_values": alignment_data}
        report_info.update(alignment_dict)
    print(request.data["basecalledSummaryHTML"])
    print(request.data["liveEventSummaryHTML"])
    report_info.update(request.data)
    cent_df = get_metagenomics_data(task, flowcell)
    cent_df.to_csv(f"/tmp/meta_csv_{run.name}.csv.gz", encoding="utf-8")
    HTML(
        string=render(request,
                      "metagenomics/report.html",
                      context={
                          "report_data": report_info
                      }).getvalue()
    ).write_pdf(
        f"/tmp/{task.id}_run_{run.name}_report.pdf",
        stylesheets=[
            CSS("web/static/web/css/report.css"),
            CSS("web/static/web/libraries/bootstrap-4.5.0-dist/css/bootstrap.css"
                ),
        ],
    )
    tar_file_path = f"/tmp/{task.id}_run_{run.name}_report.tar.gz"
    tar_file_name = f"{task.id}_run_{run.name}_report.tar.gz"
    with tarfile.open(tar_file_path, "w:gz") as tar:
        try:
            tar.add(f"/tmp/{task.id}_run_{run.name}_report.pdf",
                    recursive=False)
            tar.add(f"/tmp/meta_csv_{run.name}.csv.gz", recursive=False)
        except FileNotFoundError as e:
            print("file not found")

    with open(tar_file_path, "rb") as fh:
        response = HttpResponse(fh.read(), content_type="application/zip")
        response[
            "Content-Disposition"] = f"attachment; filename={tar_file_name}"
        response["x-file-name"] = tar_file_name

    return response
Exemplo n.º 30
0
    def mk_report(self, start_time, end_time, profeat, path=None):
        """Produce PDF report.

        Args:
          start_time (int): start time UNIX timestamp.
          end_time (int): end time UNIX timestamp.
          profeat (dict): profile features.
          path (string): report file path (opt).

        Returns:
          None: writes PDF report.
        """

        # From time to timestamp
        start_time = datetime.datetime.fromtimestamp(start_time)
        start_time = start_time.strftime('%Y-%m-%d %H:%M:%S')
        end_time = datetime.datetime.fromtimestamp(end_time)
        end_time = end_time.strftime('%Y-%m-%d %H:%M:%S')

        # Load template
        gpdir = pkg_resources.resource_filename(const.PACK_NAME, '')
        gpdir += '/static/'
        env = Environment(loader=FileSystemLoader(gpdir))
        env.filters['type'] = type
        env.filters['has_key'] = lambda a, b: b in a.keys()
        template = env.get_template("report_template.html")

        # Prepare variables to fill the template
        tempv = {
            'profeat':
            profeat,
            'starttime':
            start_time,
            'endtime':
            end_time,
            'basedir':
            self.basedir,
            'outdir':
            self.outdir,
            'logpath':
            self.logpath,
            'reg':
            self.reg,
            'ext':
            self.ext,
            'skip': [
                const.STEP_DESCR[i - 1] for i in self.skip
                if i in range(len(const.STEP_DESCR))
            ],
            'ncores':
            self.ncores,
            #'correctca' : self.correctCA,
            'verbose':
            self.verbose,
            'debugging':
            self.debugging,
            'suffix':
            self.suffix,
            'dna_names':
            self.dna_names,
            'sig_names':
            self.sig_names,
            'plotting':
            self.plotting,
            'fontsize':
            self.font_size,
            'nbins':
            self.nbins,
            'sigma_smooth':
            self.sigma_smooth,
            'sigma_density':
            self.sigma_density,
            'seg_type':
            const.SEG_LABELS[self.seg_type],
            'adp_thr':
            self.adaptive_neighbourhood,
            'radius_interval':
            self.radius_interval,
            'min_z_size':
            self.min_z_size,
            'offset':
            self.offset,
            'rm_z_tips':
            self.do_clear_Z_borders,
            'an_type':
            const.AN_LABELS[self.an_type],
            'mid_type':
            const.MID_SEC_LABELS[self.mid_type],
            'dist_type':
            self.dist_type,
            'aspect':
            self.aspect,
            'nsf': [
                const.NSEL_NAMES[i] for i in self.nsf
                if i in range(len(const.NSEL_NAMES))
            ],
            'part_n_erosion':
            self.part_n_erosion,
            'norm_d':
            self.normalize_distance,
            'rescale_deconvolved':
            self.rescale_deconvolved,
            'notes':
            self.notes,
            'conds':
            self.conds,
            'cnuclei':
            [sum([len(s.nuclei) for s in c.series]) for c in self.conds],
            'cdescr':
            self.cdescr
        }

        # Escape characters
        for (k, v) in tempv.items():
            if type('') == type(v):
                tempv[k] = v.replace('<', '&lt;').replace('>', '&gt;')

        # Fill template
        html_out = template.render(tempv)

        # Hide CSS warnings
        logger = logging.getLogger('weasyprint')
        logger.handlers = []
        logger.addHandler(logging.FileHandler('/tmp/weasyprint.log'))

        # Output
        suffix = datetime.datetime.fromtimestamp(time.time())
        suffix = suffix.strftime('%Y-%m-%d %H:%M:%S')
        fname = self.outdir + 'report.' + suffix + '.pdf'
        HTML(string=html_out).write_pdf(fname)