2012-03-22 02:19:27 +04:00
|
|
|
|
"""
|
|
|
|
|
weasyprint.__main__
|
|
|
|
|
-------------------
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
Command-line interface to WeasyPrint.
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
2017-03-25 01:12:04 +03:00
|
|
|
|
import logging
|
2020-01-11 20:33:59 +03:00
|
|
|
|
import platform
|
2017-03-25 02:33:36 +03:00
|
|
|
|
import sys
|
2021-04-28 16:14:59 +03:00
|
|
|
|
import warnings
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
2020-04-18 01:19:35 +03:00
|
|
|
|
import pydyf
|
2019-09-13 11:01:45 +03:00
|
|
|
|
|
2020-05-30 02:27:13 +03:00
|
|
|
|
from . import HTML, LOGGER, __version__
|
2021-02-08 02:53:27 +03:00
|
|
|
|
from .text.ffi import pango
|
2019-09-13 11:01:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PrintInfo(argparse.Action):
|
|
|
|
|
def __call__(*_, **__):
|
2020-01-11 20:33:59 +03:00
|
|
|
|
uname = platform.uname()
|
|
|
|
|
print('System:', uname.system)
|
2019-09-13 11:01:45 +03:00
|
|
|
|
print('Machine:', uname.machine)
|
|
|
|
|
print('Version:', uname.version)
|
|
|
|
|
print('Release:', uname.release)
|
|
|
|
|
print()
|
2020-05-30 02:27:13 +03:00
|
|
|
|
print('WeasyPrint version:', __version__)
|
2019-09-13 11:01:45 +03:00
|
|
|
|
print('Python version:', sys.version.split()[0])
|
2020-04-18 01:19:35 +03:00
|
|
|
|
print('Pydyf version:', pydyf.__version__)
|
2019-09-13 11:01:45 +03:00
|
|
|
|
print('Pango version:', pango.pango_version())
|
|
|
|
|
sys.exit()
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
|
|
|
|
|
2012-06-02 08:28:44 +04:00
|
|
|
|
def main(argv=None, stdout=None, stdin=None):
|
2012-10-08 21:51:18 +04:00
|
|
|
|
"""The ``weasyprint`` program takes at least two arguments:
|
|
|
|
|
|
|
|
|
|
.. code-block:: sh
|
|
|
|
|
|
|
|
|
|
weasyprint [options] <input> <output>
|
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
The input is a filename or URL to an HTML document, or ``-`` to read
|
|
|
|
|
HTML from stdin. The output is a filename, or ``-`` to write to stdout.
|
|
|
|
|
|
2017-04-28 21:36:14 +03:00
|
|
|
|
Options can be mixed anywhere before, between, or after the input and
|
2019-02-22 13:34:46 +03:00
|
|
|
|
output.
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: -e <input_encoding>, --encoding <input_encoding>
|
|
|
|
|
|
2017-04-28 21:36:14 +03:00
|
|
|
|
Force the input character encoding (e.g. ``-e utf8``).
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: -f <output_format>, --format <output_format>
|
|
|
|
|
|
2017-04-28 21:36:14 +03:00
|
|
|
|
Choose the output file format among PDF and PNG (e.g. ``-f png``).
|
2012-09-19 19:37:52 +04:00
|
|
|
|
Required if the output is not a ``.pdf`` or ``.png`` filename.
|
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: -s <filename_or_URL>, --stylesheet <filename_or_URL>
|
|
|
|
|
|
2019-02-22 13:34:46 +03:00
|
|
|
|
Filename or URL of a user cascading stylesheet (see
|
2021-02-18 23:03:40 +03:00
|
|
|
|
:ref:`Stylesheet Origins`) to add to the document
|
2017-04-28 21:36:14 +03:00
|
|
|
|
(e.g. ``-s print.css``). Multiple stylesheets are allowed.
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2019-01-04 03:22:49 +03:00
|
|
|
|
.. option:: -m <type>, --media-type <type>
|
|
|
|
|
|
|
|
|
|
Set the media type to use for ``@media``. Defaults to ``print``.
|
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: -r <dpi>, --resolution <dpi>
|
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
For PNG output only. Set the resolution in PNG pixel per CSS inch.
|
|
|
|
|
Defaults to 96, which means that PNG pixels match CSS pixels.
|
|
|
|
|
|
2019-01-04 03:22:49 +03:00
|
|
|
|
.. option:: -u <URL>, --base-url <URL>
|
2012-10-07 16:54:39 +04:00
|
|
|
|
|
|
|
|
|
Set the base for relative URLs in the HTML input.
|
|
|
|
|
Defaults to the input’s own URL, or the current directory for stdin.
|
|
|
|
|
|
2014-04-04 14:32:21 +04:00
|
|
|
|
.. option:: -a <file>, --attachment <file>
|
|
|
|
|
|
2017-04-28 21:36:14 +03:00
|
|
|
|
Adds an attachment to the document. The attachment is
|
2021-04-28 15:55:52 +03:00
|
|
|
|
included in the PDF output. This option can be used multiple
|
2017-04-28 21:36:14 +03:00
|
|
|
|
times.
|
2014-04-04 14:32:21 +04:00
|
|
|
|
|
2016-08-30 19:15:30 +03:00
|
|
|
|
.. option:: -p, --presentational-hints
|
|
|
|
|
|
2019-02-22 13:34:46 +03:00
|
|
|
|
Follow `HTML presentational hints
|
|
|
|
|
<https://www.w3.org/TR/html/rendering.html\
|
|
|
|
|
#the-css-user-agent-style-sheet-and-presentational-hints>`_.
|
2016-08-30 19:15:30 +03:00
|
|
|
|
|
2021-04-28 15:55:52 +03:00
|
|
|
|
.. option:: -O <type>, --optimize-size <type>
|
2020-06-22 17:05:14 +03:00
|
|
|
|
|
2021-04-28 15:55:52 +03:00
|
|
|
|
Optimize the size of generated documents. Supported types are
|
|
|
|
|
``images``, ``fonts``, ``all`` and ``none``. This option can be used
|
|
|
|
|
multiple times, ``all`` adds all allowed values, ``none`` removes all
|
|
|
|
|
previously set values.
|
2020-06-22 17:05:14 +03:00
|
|
|
|
|
2019-01-04 03:22:49 +03:00
|
|
|
|
.. option:: -v, --verbose
|
|
|
|
|
|
|
|
|
|
Show warnings and information messages.
|
|
|
|
|
|
|
|
|
|
.. option:: -d, --debug
|
|
|
|
|
|
|
|
|
|
Show debugging messages.
|
|
|
|
|
|
2020-06-23 23:35:00 +03:00
|
|
|
|
.. option:: -q, --quiet
|
|
|
|
|
|
|
|
|
|
Hide logging messages.
|
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: --version
|
|
|
|
|
|
2012-10-08 21:51:18 +04:00
|
|
|
|
Show the version number. Other options and arguments are ignored.
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-10-07 16:54:39 +04:00
|
|
|
|
.. option:: -h, --help
|
|
|
|
|
|
2012-10-08 21:51:18 +04:00
|
|
|
|
Show the command-line usage. Other options and arguments are ignored.
|
2012-10-07 16:54:39 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
"""
|
2013-04-11 14:08:53 +04:00
|
|
|
|
parser = argparse.ArgumentParser(
|
2021-04-02 17:23:28 +03:00
|
|
|
|
prog='weasyprint', description='Renders web pages to PDF.')
|
2012-02-14 15:33:17 +04:00
|
|
|
|
parser.add_argument('--version', action='version',
|
2020-05-30 02:27:13 +03:00
|
|
|
|
version=f'WeasyPrint version {__version__}',
|
2013-10-14 18:07:05 +04:00
|
|
|
|
help="Print WeasyPrint's version number and exit.")
|
2019-09-13 11:01:45 +03:00
|
|
|
|
parser.add_argument('-i', '--info', action=PrintInfo, nargs=0,
|
|
|
|
|
help='Print system information and exit.')
|
2012-02-14 15:33:17 +04:00
|
|
|
|
parser.add_argument('-e', '--encoding',
|
|
|
|
|
help='Character encoding of the input')
|
|
|
|
|
parser.add_argument('-s', '--stylesheet', action='append',
|
2012-08-02 19:19:52 +04:00
|
|
|
|
help='URL or filename for a user CSS stylesheet. '
|
2012-02-14 15:33:17 +04:00
|
|
|
|
'May be given multiple times.')
|
2012-08-02 15:04:31 +04:00
|
|
|
|
parser.add_argument('-m', '--media-type', default='print',
|
2012-08-02 19:19:52 +04:00
|
|
|
|
help='Media type to use for @media, defaults to print')
|
2019-01-04 03:22:49 +03:00
|
|
|
|
parser.add_argument('-u', '--base-url',
|
2012-08-02 19:19:52 +04:00
|
|
|
|
help='Base for relative URLs in the HTML input. '
|
|
|
|
|
"Defaults to the input's own filename or URL "
|
|
|
|
|
'or the current directory for stdin.')
|
2014-04-04 14:32:21 +04:00
|
|
|
|
parser.add_argument('-a', '--attachment', action='append',
|
2014-04-27 21:16:14 +04:00
|
|
|
|
help='URL or filename of a file '
|
|
|
|
|
'to attach to the PDF document')
|
2016-08-30 19:15:30 +03:00
|
|
|
|
parser.add_argument('-p', '--presentational-hints', action='store_true',
|
|
|
|
|
help='Follow HTML presentational hints.')
|
2021-04-28 15:55:52 +03:00
|
|
|
|
parser.add_argument('-O', '--optimize-size', action='append',
|
|
|
|
|
help='Optimize output size for specified features.',
|
|
|
|
|
choices=('images', 'fonts', 'all', 'none'),
|
|
|
|
|
default=['fonts'])
|
2017-07-25 15:00:35 +03:00
|
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
2019-01-04 03:22:49 +03:00
|
|
|
|
help='Show warnings and information messages.')
|
|
|
|
|
parser.add_argument('-d', '--debug', action='store_true',
|
|
|
|
|
help='Show debugging messages.')
|
2019-02-07 19:10:45 +03:00
|
|
|
|
parser.add_argument('-q', '--quiet', action='store_true',
|
|
|
|
|
help='Hide logging messages.')
|
2021-04-28 16:14:59 +03:00
|
|
|
|
parser.add_argument('-o', '--optimize-images', action='store_true',
|
|
|
|
|
help='Deprecated, use "-O images" instead.')
|
|
|
|
|
parser.add_argument('-f', '--format', choices=('pdf',),
|
|
|
|
|
help='Deprecated.')
|
|
|
|
|
parser.add_argument('-r', '--resolution', type=float,
|
|
|
|
|
help='Deprecated.')
|
2013-04-11 14:08:53 +04:00
|
|
|
|
parser.add_argument(
|
|
|
|
|
'input', help='URL or filename of the HTML input, or - for stdin')
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'output', help='Filename where output is written, or - for stdout')
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
2012-02-16 21:28:01 +04:00
|
|
|
|
args = parser.parse_args(argv)
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
|
|
|
|
if args.input == '-':
|
2020-01-11 20:33:59 +03:00
|
|
|
|
source = stdin or sys.stdin.buffer
|
2017-11-16 19:41:27 +03:00
|
|
|
|
if args.base_url is None:
|
2012-08-02 19:19:52 +04:00
|
|
|
|
args.base_url = '.' # current directory
|
2017-11-16 19:41:27 +03:00
|
|
|
|
elif args.base_url == '':
|
|
|
|
|
args.base_url = None # no base URL
|
2012-02-15 21:49:37 +04:00
|
|
|
|
else:
|
2012-02-27 18:07:41 +04:00
|
|
|
|
source = args.input
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
|
|
|
|
if args.output == '-':
|
2020-01-11 20:33:59 +03:00
|
|
|
|
output = stdout or sys.stdout.buffer
|
2012-02-27 17:51:10 +04:00
|
|
|
|
else:
|
|
|
|
|
output = args.output
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
2021-04-28 15:55:52 +03:00
|
|
|
|
optimize_size = set()
|
|
|
|
|
for arg in args.optimize_size:
|
|
|
|
|
if arg == 'none':
|
|
|
|
|
optimize_size.clear()
|
|
|
|
|
elif arg == 'all':
|
|
|
|
|
optimize_size |= {'images', 'fonts'}
|
|
|
|
|
else:
|
|
|
|
|
optimize_size.add(arg)
|
|
|
|
|
|
2021-04-28 16:14:59 +03:00
|
|
|
|
if args.optimize_images:
|
|
|
|
|
optimize_size.add('images')
|
|
|
|
|
|
|
|
|
|
if any((args.optimize_images, args.format, args.resolution)):
|
|
|
|
|
warnings.warn(
|
|
|
|
|
'--optimize-images, --format and --resolution options are '
|
|
|
|
|
'deprecated and will be removed in future versions.',
|
|
|
|
|
FutureWarning)
|
|
|
|
|
|
2016-08-30 20:10:53 +03:00
|
|
|
|
kwargs = {
|
|
|
|
|
'stylesheets': args.stylesheet,
|
2020-06-22 17:05:14 +03:00
|
|
|
|
'presentational_hints': args.presentational_hints,
|
2021-04-28 15:55:52 +03:00
|
|
|
|
'optimize_size': tuple(optimize_size),
|
2021-01-20 17:41:59 +03:00
|
|
|
|
'attachments': args.attachment}
|
2014-04-04 14:32:21 +04:00
|
|
|
|
|
2017-03-25 01:12:04 +03:00
|
|
|
|
# Default to logging to stderr.
|
2019-01-04 03:22:49 +03:00
|
|
|
|
if args.debug:
|
2017-07-25 14:59:56 +03:00
|
|
|
|
LOGGER.setLevel(logging.DEBUG)
|
2019-01-04 03:22:49 +03:00
|
|
|
|
elif args.verbose:
|
|
|
|
|
LOGGER.setLevel(logging.INFO)
|
2019-02-07 19:10:45 +03:00
|
|
|
|
if not args.quiet:
|
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
|
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
|
|
|
|
|
LOGGER.addHandler(handler)
|
2017-03-25 01:12:04 +03:00
|
|
|
|
|
2012-08-02 19:19:52 +04:00
|
|
|
|
html = HTML(source, base_url=args.base_url, encoding=args.encoding,
|
2016-08-30 20:10:53 +03:00
|
|
|
|
media_type=args.media_type)
|
2021-01-20 17:41:59 +03:00
|
|
|
|
html.write_pdf(output, **kwargs)
|
2012-02-14 15:33:17 +04:00
|
|
|
|
|
|
|
|
|
|
2012-04-02 16:45:44 +04:00
|
|
|
|
if __name__ == '__main__': # pragma: no cover
|
2012-02-17 18:00:55 +04:00
|
|
|
|
main()
|