-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathapi.py
More file actions
89 lines (66 loc) · 4.37 KB
/
api.py
File metadata and controls
89 lines (66 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
from .pdfkit import PDFKit
from .pdfkit import Configuration
def from_url(url, output_path=None, options=None, toc=None, cover=None,
configuration=None, cover_first=False, verbose=False, timeout=None):
"""
Convert file of files from URLs to PDF document
:param url: URL or list of URLs to be saved
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
return r.to_pdf(output_path)
def from_file(input, output_path=None, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False, verbose=False, timeout=None):
"""
Convert HTML file or files to PDF document
:param input: path to HTML file or list with paths or file-like object
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) string with path to css file which will be added to a single input file
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
return r.to_pdf(output_path)
def from_string(input, output_path=None, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False, verbose=False, timeout=None):
"""
Convert given string or strings to PDF document
:param input: string with a desired text. Could be a raw text or a html file
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) string with path to css file which will be added to a input string
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
return r.to_pdf(output_path)
def configuration(**kwargs):
"""
Constructs and returns a :class:`Configuration` with given options
:param wkhtmltopdf: path to binary
:param meta_tag_prefix: the prefix for ``pdfkit`` specific meta tags
"""
return Configuration(**kwargs)