Python pdfkit.from_string() Examples

The following are 10 code examples of pdfkit.from_string(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pdfkit , or try the search function .
Example #1
Source File: main.py    From Deerlet with GNU General Public License v2.0 7 votes vote down vote up
def download():
    read_password = current_app.config.get('READ_PASSWORD')
    if not read_password or session.get('read_password') == read_password:
        input_filename = 'resume.md'
        output_filename = 'resume.pdf'

        with open(input_filename, 'r') as stream:
            html_text = markdown(stream.read(), output_format='html4')
        # render the html template
        output = render_template('pdf_template.html', yue_css=yue_css,
                                 title=current_app.config.get('TITLE'),
                                 sub_title=current_app.config.get('SUB_TITLE'),
                                 content=html_text)

        # generate pdf file
        pdfkit.from_string(output, output_filename, options=current_app.config.get('PDF_OPTIONS'), )

        return send_from_directory(current_app.config.get('UPLOAD_FOLDER'),
                                   'resume.pdf', as_attachment=True)
    else:
        return redirect(url_for("resume")) 
Example #2
Source File: markdown_to_pdf.py    From recordexpungPDX with MIT License 7 votes vote down vote up
def to_pdf(title: str, markdown_source: str) -> bytes:
        html_style = MarkdownToPDF.css()
        html_body = markdown2.markdown(markdown_source)
        html = f"""
        <html>
        <head>
            <meta charset="utf-8">
            <title>{title}</title>
            <style>
                {html_style}
            </style>
        </head>
        <body class="markdown-body">
            {html_body}
        </body>
        </html>
        """
        return pdfkit.from_string(html, False, options={"quiet": ""}) 
Example #3
Source File: xzl.py    From xzl with MIT License 6 votes vote down vote up
def get_xs_detail(href, title, path):
    url = xzl+href
    print('开始采集' + title + '的详情, 章节地址为: ' + url + '\n')
    text_maker = ht.HTML2Text()
    response = close_session().get(url=url, headers=headers)
    selector = Selector(text=response.text)
    html = selector.css(u'.cata-book-content').extract_first()
    file_name = title
    if markdown:
        md = text_maker.handle(html)
        with open(path + file_name + '.md', 'w') as f:
            f.write(md)
    else:
        if not xs_pdf:
            # 在html中加入编码, 否则中文会乱码
            html = "<html><head><meta charset='utf-8'></head> " + html + "</html>"
            pdfkit.from_string(html, path + file_name + '.pdf')
        else:
            return html


# 采集专栏列表 
Example #4
Source File: xzl.py    From xzl with MIT License 6 votes vote down vote up
def get_zl_detail(url, path, name):
    response = close_session().get(url=url, headers=headers)
    selector = Selector(text=response.text)
    text_maker = ht.HTML2Text()
    create_time = selector.css(u'.time abbr::attr(title)').extract_first()
    html = selector.css(u'.xzl-topic-body-content').extract_first()
    file_name = name
    if hasTime:
        file_name = create_time+' '+name
    if markdown:
        md = text_maker.handle(html)
        with open(path + file_name + '.md', 'w') as f:
            f.write(md)
    else:
        # 在html中加入编码, 否则中文会乱码
        html = "<html><head><meta charset='utf-8'></head> " + html + "</html>"
        pdfkit.from_string(html, path + file_name + '.pdf')


# 关闭多余连接 
Example #5
Source File: md2pdf.py    From portal-andino with MIT License 5 votes vote down vote up
def main(input_paths_str, output_path):

    # lee los htmls a convertir en PDF
    input_paths = input_paths_str.split(",")
    htmls = []
    for input_path in input_paths:
        with open(input_path) as input_file:
            htmls.append(markdown2.markdown(
                input_file.read(),
                extras=["fenced_code", "codehilite", "admonition"]))
    print("Hay {} documentos".format(len(htmls)))

    # guarda html
    with open(output_path.replace(".pdf", ".html"), "wb") as output_html:

        # aplica el estilo al principio
        html = "\n".join(htmls)
        html_with_style = """
        <link rel="stylesheet" href="pdf.css" type="text/css"/>
        """ + html

        # escribe el html
        output_html.write(html_with_style.encode("utf-8"))
        shutil.copyfile(
            "docs/css/pdf.css",
            os.path.join(os.path.dirname(output_path), "pdf.css")
        )

    # guarda pdf
    pdfkit.from_string(html, output_path, options={"encoding": "utf8"},
                       css="docs/css/pdf.css") 
Example #6
Source File: utils.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def download_pdf(data: pd.DataFrame, file_name='output'):
    data_html = data.to_html(index=False)
    try:
        data_pdf = pdf.from_string(data_html, False)
    except OSError:
        env = Environment(loader=FileSystemLoader(settings.PROJECT_DIR('templates')))
        template = env.get_template('pdf_export.html')
        template_vars = {"title": file_name.capitalize(),
                         "table": data_html}
        data_pdf = HTML(string=template.render(template_vars)).write_pdf()
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="{}.{}"'.format(file_name, 'pdf')
    response.write(data_pdf)
    return response 
Example #7
Source File: pdf_utils.py    From django-htk with MIT License 5 votes vote down vote up
def render_to_pdf_response_pdfkit(template_name, context_dict, css_files=None):
    """Render to a PDF response using pdfkit

    `context_dict` is expected to be generated by htk.view_helpers.wrap_data

    PyPI: https://pypi.python.org/pypi/pdfkit
    Installation: https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server

    Outstanding Issues:
    - 
    """
    import pdfkit
    html = generate_html_from_template(template_name, context_dict)
    base_url = context_dict['request']['base_uri']
    #html = rewrite_relative_urls_as_absolute(html, base_url)
    options = {
        'page-size' : 'Letter',
        'orientation' : 'Portrait',
        'margin-top' : '0.75in',
        'margin-bottom' : '0.75in',
        'margin-left' : '0.50in',
        'margin-right' : '0.50in',
        'encoding' : 'UTF-8',
        #'print_media_type' : False,
        #'title' : context_dict.get('title', 'PDF'),
    }
    pdf = pdfkit.from_string(html.encode('utf-8'), False, options=options, css=css_files)
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
    else:
        response = HttpResponseServerError('Error generating PDF file')
    return response 
Example #8
Source File: html_to_pdf.py    From python-tools with MIT License 4 votes vote down vote up
def html_to_pdf(input, output, type='string'):
    """Convert HTML/webpage to PDF. For linux, install: sudo apt-get install wkhtmltopdf.

    Args:
        input (str): HTML Text, URL or file.
        output (str): Output file (.pdf).
        type (str): Types can be 'string', 'url' or 'file'.

    """
    if type == 'url':
        pdfkit.from_url(input, output)
    elif type == 'file':
        pdfkit.from_file(input, output)
    else:
        pdfkit.from_string(input, output) 
Example #9
Source File: RemarkableWindow.py    From Remarkable with MIT License 4 votes vote down vote up
def save_pdf(self, html):
        chooser = Gtk.FileChooserDialog("Export PDF", None, Gtk.FileChooserAction.SAVE,
                                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                         Gtk.STOCK_OK, Gtk.ResponseType.OK))
        self.set_file_chooser_path(chooser)
        pdf_filter = Gtk.FileFilter()
        pdf_filter.add_pattern("*.pdf")
        pdf_filter.set_name("PDF Files")
        chooser.add_filter(pdf_filter)
        chooser.set_do_overwrite_confirmation(True)
        response = chooser.run()
        if response == Gtk.ResponseType.OK:
            file_name = chooser.get_filename()
            if not file_name.endswith(".pdf"):
                file_name += ".pdf"
            try:
                pdfkit.from_string(html, file_name, options= {'quiet': '', 'page-size': 'Letter',
                    'margin-top': '0.75in',
                    'margin-right': '0.75in',
                    'margin-bottom': '0.75in',
                    'margin-left': '0.75in',
                    'encoding': "UTF-8",
                    'javascript-delay' : '550',
                    'no-outline': None})
            except:
                try:
                    # Failed so try with no options
                    pdfkit.from_string(html, file_name)
                except:
                    # Pdf Export failed, show warning message
                    if not self.pdf_error_warning:
                        self.pdf_error_warning = True
                        print("\nRemarkable Error:\tPDF Export Failed!!")

                    pdf_fail_dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR,
                            Gtk.ButtonsType.CANCEL, "PDF EXPORT FAILED")
                    pdf_fail_dialog.format_secondary_text(
                            "File export to PDF was unsuccessful.")
                    pdf_fail_dialog.run()
                    pdf_fail_dialog.destroy()
        elif response == Gtk.ResponseType.CANCEL:
            pass

        chooser.destroy()
        self.window.set_sensitive(True) 
Example #10
Source File: xzl.py    From xzl with MIT License 4 votes vote down vote up
def get_xs(href, is_all=False):
    url = xzl + href + '#a4'
    print('开始采集小书信息,小书地址为: ' + url + '\n')
    xzl_path = ''
    if is_all:
        xzl_path = '小专栏/'
    response = close_session().get(url=url, headers=headers)
    selector = Selector(text=response.text)
    chapter = selector.css(u'.book-cata-item').extract()
    xs_title = selector.css(u'.bannerMsg .title ::text').extract_first()
    html = ''
    if xs_pdf:
        html = '<div>' + selector.css(u'.dot-list').extract_first() + '</div>'
    for idx, c in enumerate(chapter):
        selector = Selector(text=c)
        items = selector.css(u'.cata-sm-item').extract()
        z_title = selector.css(u'a::text').extract_first()
        z_href = selector.css(u'a::attr(href)').extract_first()
        path = os.path.join(os.path.expanduser("~"), 'Desktop')+'/'+xzl_path+xs_title+'/'+z_title+'/'
        if xs_pdf:
            path = os.path.join(os.path.expanduser("~"), 'Desktop')+'/'+xzl_path+xs_title+'/'
        else:
            print(xs_title + '共%d章, 正在创建存储目录\n' % len(chapter))
            print('文件存储位置: ' + path + '\n')
        if not os.path.exists(path):
            os.makedirs(path)
            print('文件夹创建成功\n')
        html += get_xs_detail(z_href, z_title, path)
        for item in items:
            selector = Selector(text=item)
            j_title = selector.css(u'.cata-sm-item a::text').extract_first()
            j_href = selector.css(u'.cata-sm-item a::attr(href)').extract_first()
            html += get_xs_detail(j_href, j_title, path)
            time.sleep(seconds)
        time.sleep(seconds)
    if xs_pdf:
        # 在html中加入编码, 否则中文会乱码
        html = "<html><head><meta charset='utf-8'></head> " + html + "</html>"
        pdfkit.from_string(html, path+xs_title+'.pdf')
    print('小书:' + xs_title + '的文章已采集完成\n')
    print('我们应该尊重每一位作者的付出, 请不要随意传播下载后的文件\n')


# 采集小书章节详情