WeasyPrint-51/ 0000755 0001750 0001750 00000000000 13600171153 013333 5 ustar lize lize 0000000 0000000 WeasyPrint-51/LICENSE 0000644 0001750 0001750 00000003014 13577514116 014353 0 ustar lize lize 0000000 0000000 BSD 3-Clause License
Copyright (c) 2011-2019, Simon Sapin and contributors (see AUTHORS).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
WeasyPrint-51/PKG-INFO 0000644 0001750 0001750 00000004504 13600171153 014433 0 ustar lize lize 0000000 0000000 Metadata-Version: 2.1
Name: WeasyPrint
Version: 51
Summary: The Awesome Document Factory
Home-page: https://weasyprint.org/
Author: Simon Sapin
Author-email: community@kozea.fr
License: BSD
Project-URL: Documentation, https://weasyprint.readthedocs.io/
Project-URL: Code, https://github.com/Kozea/WeasyPrint/
Project-URL: Issue tracker, https://github.com/Kozea/WeasyPrint/issues
Project-URL: Donation, https://www.patreon.com/kozea
Description: ==========
WeasyPrint
==========
**The Awesome Document Factory**
WeasyPrint is a smart solution helping web developers to create PDF
documents. It turns simple HTML pages into gorgeous statistical reports,
invoices, tickets…
From a technical point of view, WeasyPrint is a visual rendering engine for
HTML and CSS that can export to PDF and PNG. It aims to support web standards
for printing. WeasyPrint is free software made available under a BSD license.
It is based on various libraries but *not* on a full rendering engine like
WebKit or Gecko. The CSS layout engine is written in Python, designed for
pagination, and meant to be easy to hack on.
* Free software: BSD licensed
* Python 3.5+
* Website: https://weasyprint.org/
* Documentation: https://weasyprint.readthedocs.io/
* Source code and issue tracker: https://github.com/Kozea/WeasyPrint
* Tests: https://travis-ci.org/Kozea/WeasyPrint
* Support: https://www.patreon.com/kozea
Keywords: html,css,pdf,converter
Platform: Linux
Platform: macOS
Platform: Windows
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Text Processing :: Markup :: HTML
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
Classifier: Topic :: Printing
Requires-Python: >=3.5
Description-Content-Type: text/x-rst
Provides-Extra: doc
Provides-Extra: test
WeasyPrint-51/README.rst 0000644 0001750 0001750 00000001664 13577514116 015046 0 ustar lize lize 0000000 0000000 ==========
WeasyPrint
==========
**The Awesome Document Factory**
WeasyPrint is a smart solution helping web developers to create PDF
documents. It turns simple HTML pages into gorgeous statistical reports,
invoices, tickets…
From a technical point of view, WeasyPrint is a visual rendering engine for
HTML and CSS that can export to PDF and PNG. It aims to support web standards
for printing. WeasyPrint is free software made available under a BSD license.
It is based on various libraries but *not* on a full rendering engine like
WebKit or Gecko. The CSS layout engine is written in Python, designed for
pagination, and meant to be easy to hack on.
* Free software: BSD licensed
* Python 3.5+
* Website: https://weasyprint.org/
* Documentation: https://weasyprint.readthedocs.io/
* Source code and issue tracker: https://github.com/Kozea/WeasyPrint
* Tests: https://travis-ci.org/Kozea/WeasyPrint
* Support: https://www.patreon.com/kozea
WeasyPrint-51/WeasyPrint.egg-info/ 0000755 0001750 0001750 00000000000 13600171153 017132 5 ustar lize lize 0000000 0000000 WeasyPrint-51/WeasyPrint.egg-info/PKG-INFO 0000644 0001750 0001750 00000004504 13600171153 020232 0 ustar lize lize 0000000 0000000 Metadata-Version: 2.1
Name: WeasyPrint
Version: 51
Summary: The Awesome Document Factory
Home-page: https://weasyprint.org/
Author: Simon Sapin
Author-email: community@kozea.fr
License: BSD
Project-URL: Documentation, https://weasyprint.readthedocs.io/
Project-URL: Code, https://github.com/Kozea/WeasyPrint/
Project-URL: Issue tracker, https://github.com/Kozea/WeasyPrint/issues
Project-URL: Donation, https://www.patreon.com/kozea
Description: ==========
WeasyPrint
==========
**The Awesome Document Factory**
WeasyPrint is a smart solution helping web developers to create PDF
documents. It turns simple HTML pages into gorgeous statistical reports,
invoices, tickets…
From a technical point of view, WeasyPrint is a visual rendering engine for
HTML and CSS that can export to PDF and PNG. It aims to support web standards
for printing. WeasyPrint is free software made available under a BSD license.
It is based on various libraries but *not* on a full rendering engine like
WebKit or Gecko. The CSS layout engine is written in Python, designed for
pagination, and meant to be easy to hack on.
* Free software: BSD licensed
* Python 3.5+
* Website: https://weasyprint.org/
* Documentation: https://weasyprint.readthedocs.io/
* Source code and issue tracker: https://github.com/Kozea/WeasyPrint
* Tests: https://travis-ci.org/Kozea/WeasyPrint
* Support: https://www.patreon.com/kozea
Keywords: html,css,pdf,converter
Platform: Linux
Platform: macOS
Platform: Windows
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Text Processing :: Markup :: HTML
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
Classifier: Topic :: Printing
Requires-Python: >=3.5
Description-Content-Type: text/x-rst
Provides-Extra: doc
Provides-Extra: test
WeasyPrint-51/WeasyPrint.egg-info/SOURCES.txt 0000644 0001750 0001750 00000010256 13600171153 021022 0 ustar lize lize 0000000 0000000 LICENSE
README.rst
setup.cfg
setup.py
WeasyPrint.egg-info/PKG-INFO
WeasyPrint.egg-info/SOURCES.txt
WeasyPrint.egg-info/dependency_links.txt
WeasyPrint.egg-info/entry_points.txt
WeasyPrint.egg-info/not-zip-safe
WeasyPrint.egg-info/requires.txt
WeasyPrint.egg-info/top_level.txt
weasyprint/VERSION
weasyprint/__init__.py
weasyprint/__main__.py
weasyprint/document.py
weasyprint/draw.py
weasyprint/fonts.py
weasyprint/html.py
weasyprint/images.py
weasyprint/logger.py
weasyprint/pdf.py
weasyprint/stacking.py
weasyprint/text.py
weasyprint/urls.py
weasyprint/css/__init__.py
weasyprint/css/computed_values.py
weasyprint/css/html5_ph.css
weasyprint/css/html5_ua.css
weasyprint/css/media_queries.py
weasyprint/css/properties.py
weasyprint/css/targets.py
weasyprint/css/tests_ua.css
weasyprint/css/utils.py
weasyprint/css/validation/__init__.py
weasyprint/css/validation/descriptors.py
weasyprint/css/validation/expanders.py
weasyprint/css/validation/properties.py
weasyprint/formatting_structure/__init__.py
weasyprint/formatting_structure/boxes.py
weasyprint/formatting_structure/build.py
weasyprint/formatting_structure/counters.py
weasyprint/layout/__init__.py
weasyprint/layout/absolute.py
weasyprint/layout/backgrounds.py
weasyprint/layout/blocks.py
weasyprint/layout/columns.py
weasyprint/layout/flex.py
weasyprint/layout/float.py
weasyprint/layout/inlines.py
weasyprint/layout/min_max.py
weasyprint/layout/pages.py
weasyprint/layout/percentages.py
weasyprint/layout/preferred.py
weasyprint/layout/replaced.py
weasyprint/layout/tables.py
weasyprint/tests/__init__.py
weasyprint/tests/test_acid2.py
weasyprint/tests/test_api.py
weasyprint/tests/test_boxes.py
weasyprint/tests/test_css.py
weasyprint/tests/test_css_descriptors.py
weasyprint/tests/test_css_validation.py
weasyprint/tests/test_float.py
weasyprint/tests/test_fonts.py
weasyprint/tests/test_pdf.py
weasyprint/tests/test_presentational_hints.py
weasyprint/tests/test_stacking.py
weasyprint/tests/test_target.py
weasyprint/tests/test_text.py
weasyprint/tests/test_tools.py
weasyprint/tests/test_unicode.py
weasyprint/tests/test_variables.py
weasyprint/tests/testing_utils.py
weasyprint/tests/resources/AHEM____.TTF
weasyprint/tests/resources/acid2-reference.html
weasyprint/tests/resources/acid2-test.html
weasyprint/tests/resources/blue.jpg
weasyprint/tests/resources/doc1.html
weasyprint/tests/resources/doc1_UTF-16BE.html
weasyprint/tests/resources/icon.png
weasyprint/tests/resources/latin1-test.css
weasyprint/tests/resources/logo_small.png
weasyprint/tests/resources/mini_ua.css
weasyprint/tests/resources/pattern.gif
weasyprint/tests/resources/pattern.palette.png
weasyprint/tests/resources/pattern.png
weasyprint/tests/resources/pattern.svg
weasyprint/tests/resources/really-a-png.svg
weasyprint/tests/resources/really-a-svg.png
weasyprint/tests/resources/sheet2.css
weasyprint/tests/resources/user.css
weasyprint/tests/resources/utf8-test.css
weasyprint/tests/resources/weasyprint.otf
weasyprint/tests/resources/sub_directory/sheet1.css
weasyprint/tests/test_draw/__init__.py
weasyprint/tests/test_draw/test_background.py
weasyprint/tests/test_draw/test_before_after.py
weasyprint/tests/test_draw/test_box.py
weasyprint/tests/test_draw/test_column.py
weasyprint/tests/test_draw/test_current_color.py
weasyprint/tests/test_draw/test_gradient.py
weasyprint/tests/test_draw/test_image.py
weasyprint/tests/test_draw/test_list.py
weasyprint/tests/test_draw/test_opacity.py
weasyprint/tests/test_draw/test_overflow.py
weasyprint/tests/test_draw/test_table.py
weasyprint/tests/test_draw/test_text.py
weasyprint/tests/test_draw/test_transform.py
weasyprint/tests/test_draw/test_visibility.py
weasyprint/tests/test_layout/__init__.py
weasyprint/tests/test_layout/test_block.py
weasyprint/tests/test_layout/test_column.py
weasyprint/tests/test_layout/test_flex.py
weasyprint/tests/test_layout/test_image.py
weasyprint/tests/test_layout/test_inline.py
weasyprint/tests/test_layout/test_inline_block.py
weasyprint/tests/test_layout/test_list.py
weasyprint/tests/test_layout/test_page.py
weasyprint/tests/test_layout/test_position.py
weasyprint/tests/test_layout/test_shrink_to_fit.py
weasyprint/tests/test_layout/test_table.py
weasyprint/tools/__init__.py
weasyprint/tools/navigator.py
weasyprint/tools/renderer.py WeasyPrint-51/WeasyPrint.egg-info/dependency_links.txt 0000644 0001750 0001750 00000000001 13600171153 023200 0 ustar lize lize 0000000 0000000
WeasyPrint-51/WeasyPrint.egg-info/entry_points.txt 0000644 0001750 0001750 00000000071 13600171153 022426 0 ustar lize lize 0000000 0000000 [console_scripts]
weasyprint = weasyprint.__main__:main
WeasyPrint-51/WeasyPrint.egg-info/not-zip-safe 0000644 0001750 0001750 00000000001 13433301002 021350 0 ustar lize lize 0000000 0000000
WeasyPrint-51/WeasyPrint.egg-info/requires.txt 0000644 0001750 0001750 00000000335 13600171153 021533 0 ustar lize lize 0000000 0000000 setuptools>=39.2.0
cffi>=0.6
html5lib>=0.999999999
cairocffi>=0.9.0
tinycss2>=1.0.0
cssselect2>=0.1
CairoSVG>=2.4.0
Pyphen>=0.9.1
[doc]
sphinx
sphinx_rtd_theme
[test]
pytest-runner
pytest-cov
pytest-flake8
pytest-isort
WeasyPrint-51/WeasyPrint.egg-info/top_level.txt 0000644 0001750 0001750 00000000013 13600171153 021656 0 ustar lize lize 0000000 0000000 weasyprint
WeasyPrint-51/setup.cfg 0000644 0001750 0001750 00000004212 13600171153 015153 0 ustar lize lize 0000000 0000000 [metadata]
name = WeasyPrint
url = https://weasyprint.org/
version = file: weasyprint/VERSION
license = BSD
license_file = LICENSE
description = The Awesome Document Factory
long_description = file: README.rst
long_description_content_type = text/x-rst
author = Simon Sapin
author_email = community@kozea.fr
platforms =
Linux
macOS
Windows
keywords =
html
css
pdf
converter
classifiers =
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Programming Language :: Python :: 3
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Internet :: WWW/HTTP
Topic :: Text Processing :: Markup :: HTML
Topic :: Multimedia :: Graphics :: Graphics Conversion
Topic :: Printing
project_urls =
Documentation = https://weasyprint.readthedocs.io/
Code = https://github.com/Kozea/WeasyPrint/
Issue tracker = https://github.com/Kozea/WeasyPrint/issues
Donation = https://www.patreon.com/kozea
[options]
packages = find:
zip_safe = false
setup_requires = pytest-runner
install_requires =
setuptools>=39.2.0
cffi>=0.6
html5lib>=0.999999999
cairocffi>=0.9.0
tinycss2>=1.0.0
cssselect2>=0.1
CairoSVG>=2.4.0
Pyphen>=0.9.1
tests_require =
pytest-runner
pytest-cov
pytest-flake8
pytest-isort
python_requires = >= 3.5
[options.entry_points]
console-scripts = weasyprint = weasyprint.__main__:main
[options.package_data]
weasyprint = VERSION
weasyprint.tests = resources/*.*, resources/*/*
weasyprint.css = *.css
[options.extras_require]
doc =
sphinx
sphinx_rtd_theme
test =
pytest-runner
pytest-cov
pytest-flake8
pytest-isort
[bdist_wheel]
python-tag = py3
[build_sphinx]
source-dir = docs
build-dir = docs/_build
[aliases]
test = pytest
[tool:pytest]
addopts = --flake8 --isort
norecursedirs = build dist .cache .eggs .git
[coverage:run]
branch = True
include = weasyprint/*
[coverage:report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
omit =
.*
[isort]
default_section = THIRDPARTY
multi_line_output = 4
[egg_info]
tag_build =
tag_date = 0
WeasyPrint-51/setup.py 0000755 0001750 0001750 00000000731 13577514116 015066 0 ustar lize lize 0000000 0000000 #!/usr/bin/env python
"""
WeasyPrint
==========
WeasyPrint converts web documents to PDF.
:copyright: Copyright 2011-2019 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import sys
from setuptools import setup
if sys.version_info.major < 3:
raise RuntimeError(
'WeasyPrint does not support Python 2.x anymore. '
'Please use Python 3 or install an older version of WeasyPrint.')
setup()
WeasyPrint-51/weasyprint/ 0000755 0001750 0001750 00000000000 13600171153 015540 5 ustar lize lize 0000000 0000000 WeasyPrint-51/weasyprint/VERSION 0000644 0001750 0001750 00000000002 13600163110 016572 0 ustar lize lize 0000000 0000000 51 WeasyPrint-51/weasyprint/__init__.py 0000644 0001750 0001750 00000043711 13577514116 017674 0 ustar lize lize 0000000 0000000 """
WeasyPrint
==========
WeasyPrint converts web documents to PDF.
The public API is what is accessible from this "root" packages
without importing sub-modules.
:copyright: Copyright 2011-2019 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import contextlib
import os
import sys
from pathlib import Path
import cssselect2
import html5lib
import tinycss2
if sys.version_info.major < 3:
raise RuntimeError(
'WeasyPrint does not support Python 2.x anymore. '
'Please use Python 3 or install an older version of WeasyPrint.')
if hasattr(sys, 'frozen'):
if hasattr(sys, '_MEIPASS'):
# Frozen with PyInstaller
# See https://github.com/Kozea/WeasyPrint/pull/540
ROOT = Path(sys._MEIPASS)
else:
# Frozen with something else (py2exe, etc.)
# See https://github.com/Kozea/WeasyPrint/pull/269
ROOT = os.path.dirname(sys.executable)
else:
ROOT = Path(os.path.dirname(__file__))
VERSION = __version__ = (ROOT / 'VERSION').read_text().strip()
# Used for 'User-Agent' in HTTP and 'Creator' in PDF
VERSION_STRING = 'WeasyPrint %s (http://weasyprint.org/)' % VERSION
__all__ = ['HTML', 'CSS', 'Attachment', 'Document', 'Page',
'default_url_fetcher', 'VERSION']
# Import after setting the version, as the version is used in other modules
from .urls import ( # noqa isort:skip
fetch, default_url_fetcher, path2url, ensure_url, url_is_absolute)
from .logger import LOGGER, PROGRESS_LOGGER # noqa isort:skip
# Some imports are at the end of the file (after the CSS class)
# to work around circular imports.
class HTML(object):
"""Represents an HTML document parsed by html5lib.
You can just create an instance with a positional argument:
``doc = HTML(something)``
The class will try to guess if the input is a filename, an absolute URL,
or a :term:`file object`.
Alternatively, use **one** named argument so that no guessing is involved:
:type filename: str or pathlib.Path
:param filename: A filename, relative to the current directory, or
absolute.
:type url: str
:param url: An absolute, fully qualified URL.
:type file_obj: :term:`file object`
:param file_obj: Any object with a ``read`` method.
:type string: str
:param string: A string of HTML source.
Specifying multiple inputs is an error:
``HTML(filename="foo.html", url="localhost://bar.html")``
will raise a :obj:`TypeError`.
You can also pass optional named arguments:
:type encoding: str
:param encoding: Force the source character encoding.
:type base_url: str
:param base_url: The base used to resolve relative URLs
(e.g. in ````). If not provided, try to use
the input filename, URL, or ``name`` attribute of :term:`file objects
`.
:type url_fetcher: function
:param url_fetcher: A function or other callable
with the same signature as :func:`default_url_fetcher` called to
fetch external resources such as stylesheets and images.
(See :ref:`url-fetchers`.)
:type media_type: str
:param media_type: The media type to use for ``@media``.
Defaults to ``'print'``. **Note:** In some cases like
``HTML(string=foo)`` relative URLs will be invalid if ``base_url``
is not provided.
"""
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
string=None, encoding=None, base_url=None,
url_fetcher=default_url_fetcher, media_type='print'):
PROGRESS_LOGGER.info(
'Step 1 - Fetching and parsing HTML - %s',
guess or filename or url or
getattr(file_obj, 'name', 'HTML string'))
result = _select_source(
guess, filename, url, file_obj, string, base_url, url_fetcher)
with result as (source_type, source, base_url, protocol_encoding):
if isinstance(source, str):
result = html5lib.parse(source, namespaceHTMLElements=False)
else:
result = html5lib.parse(
source, override_encoding=encoding,
transport_encoding=protocol_encoding,
namespaceHTMLElements=False)
assert result
self.base_url = find_base_url(result, base_url)
self.url_fetcher = url_fetcher
self.media_type = media_type
self.wrapper_element = cssselect2.ElementWrapper.from_html_root(
result, content_language=None)
self.etree_element = self.wrapper_element.etree_element
def _ua_stylesheets(self):
return [HTML5_UA_STYLESHEET]
def _ph_stylesheets(self):
return [HTML5_PH_STYLESHEET]
def _get_metadata(self):
return get_html_metadata(self.wrapper_element, self.base_url)
def render(self, stylesheets=None, enable_hinting=False,
presentational_hints=False, font_config=None):
"""Lay out and paginate the document, but do not (yet) export it
to PDF or PNG.
This returns a :class:`~document.Document` object which provides
access to individual pages and various meta-data.
See :meth:`write_pdf` to get a PDF directly.
.. versionadded:: 0.15
:type stylesheets: list
:param stylesheets:
An optional list of user stylesheets. List elements are
:class:`CSS` objects, filenames, URLs, or file
objects. (See :ref:`stylesheet-origins`.)
:type enable_hinting: bool
:param enable_hinting:
Whether text, borders and background should be *hinted* to fall
at device pixel boundaries. Should be enabled for pixel-based
output (like PNG) but not for vector-based output (like PDF).
:type presentational_hints: bool
:param presentational_hints: Whether HTML presentational hints are
followed.
:type font_config: :class:`~fonts.FontConfiguration`
:param font_config: A font configuration handling ``@font-face`` rules.
:returns: A :class:`~document.Document` object.
"""
return Document._render(
self, stylesheets, enable_hinting, presentational_hints,
font_config)
def write_pdf(self, target=None, stylesheets=None, zoom=1,
attachments=None, presentational_hints=False,
font_config=None):
"""Render the document to a PDF file.
This is a shortcut for calling :meth:`render`, then
:meth:`Document.write_pdf() `.
:type target: str, pathlib.Path or file object
:param target:
A filename where the PDF file is generated, a file object, or
:obj:`None`.
:type stylesheets: list
:param stylesheets:
An optional list of user stylesheets. The list's elements
are :class:`CSS` objects, filenames, URLs, or file-like
objects. (See :ref:`stylesheet-origins`.)
:type zoom: float
:param zoom:
The zoom factor in PDF units per CSS units. **Warning**:
All CSS units are affected, including physical units like
``cm`` and named sizes like ``A4``. For values other than
1, the physical CSS units will thus be "wrong".
:type attachments: list
:param attachments: A list of additional file attachments for the
generated PDF document or :obj:`None`. The list's elements are
:class:`Attachment` objects, filenames, URLs or file-like objects.
:type presentational_hints: bool
:param presentational_hints: Whether HTML presentational hints are
followed.
:type font_config: :class:`~fonts.FontConfiguration`
:param font_config: A font configuration handling ``@font-face`` rules.
:returns:
The PDF as :obj:`bytes` if ``target`` is not provided or
:obj:`None`, otherwise :obj:`None` (the PDF is written to
``target``).
"""
return self.render(
stylesheets, enable_hinting=False,
presentational_hints=presentational_hints,
font_config=font_config).write_pdf(
target, zoom, attachments)
def write_image_surface(self, stylesheets=None, resolution=96,
presentational_hints=False, font_config=None):
"""Render pages vertically on a cairo image surface.
.. versionadded:: 0.17
There is no decoration around pages other than those specified in CSS
with ``@page`` rules. The final image is as wide as the widest page.
Each page is below the previous one, centered horizontally.
This is a shortcut for calling :meth:`render`, then
:meth:`Document.write_image_surface()
`.
:type stylesheets: list
:param stylesheets:
An optional list of user stylesheets. The list's elements
are :class:`CSS` objects, filenames, URLs, or file-like
objects. (See :ref:`stylesheet-origins`.)
:type resolution: float
:param resolution:
The output resolution in PNG pixels per CSS inch. At 96 dpi
(the default), PNG pixels match the CSS ``px`` unit.
:type presentational_hints: bool
:param presentational_hints: Whether HTML presentational hints are
followed.
:type font_config: :class:`~fonts.FontConfiguration`
:param font_config: A font configuration handling ``@font-face`` rules.
:returns: A cairo :class:`ImageSurface `.
"""
surface, _width, _height = (
self.render(stylesheets, enable_hinting=True,
presentational_hints=presentational_hints,
font_config=font_config)
.write_image_surface(resolution))
return surface
def write_png(self, target=None, stylesheets=None, resolution=96,
presentational_hints=False, font_config=None):
"""Paint the pages vertically to a single PNG image.
There is no decoration around pages other than those specified in CSS
with ``@page`` rules. The final image is as wide as the widest page.
Each page is below the previous one, centered horizontally.
This is a shortcut for calling :meth:`render`, then
:meth:`Document.write_png() `.
:type target: str, pathlib.Path or file object
:param target:
A filename where the PNG file is generated, a file object, or
:obj:`None`.
:type stylesheets: list
:param stylesheets:
An optional list of user stylesheets. The list's elements
are :class:`CSS` objects, filenames, URLs, or file-like
objects. (See :ref:`stylesheet-origins`.)
:type resolution: float
:param resolution:
The output resolution in PNG pixels per CSS inch. At 96 dpi
(the default), PNG pixels match the CSS ``px`` unit.
:type presentational_hints: bool
:param presentational_hints: Whether HTML presentational hints are
followed.
:type font_config: :class:`~fonts.FontConfiguration`
:param font_config: A font configuration handling ``@font-face`` rules.
:returns:
The image as :obj:`bytes` if ``target`` is not provided or
:obj:`None`, otherwise :obj:`None` (the image is written to
``target``.)
"""
png_bytes, _width, _height = (
self.render(stylesheets, enable_hinting=True,
presentational_hints=presentational_hints,
font_config=font_config)
.write_png(target, resolution))
return png_bytes
class CSS(object):
"""Represents a CSS stylesheet parsed by tinycss2.
An instance is created in the same way as :class:`HTML`, with the same
arguments.
An additional argument called ``font_config`` must be provided to handle
``@font-config`` rules. The same ``fonts.FontConfiguration`` object must be
used for different ``CSS`` objects applied to the same document.
``CSS`` objects have no public attributes or methods. They are only meant
to be used in the :meth:`~HTML.write_pdf`, :meth:`~HTML.write_png` and
:meth:`~HTML.render` methods of :class:`HTML` objects.
"""
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
string=None, encoding=None, base_url=None,
url_fetcher=default_url_fetcher, _check_mime_type=False,
media_type='print', font_config=None, matcher=None,
page_rules=None):
PROGRESS_LOGGER.info(
'Step 2 - Fetching and parsing CSS - %s',
filename or url or getattr(file_obj, 'name', 'CSS string'))
result = _select_source(
guess, filename, url, file_obj, string,
base_url=base_url, url_fetcher=url_fetcher,
check_css_mime_type=_check_mime_type)
with result as (source_type, source, base_url, protocol_encoding):
if source_type == 'string' and not isinstance(source, bytes):
# unicode, no encoding
stylesheet = tinycss2.parse_stylesheet(source)
else:
if source_type == 'file_obj':
source = source.read()
stylesheet, encoding = tinycss2.parse_stylesheet_bytes(
source, environment_encoding=encoding,
protocol_encoding=protocol_encoding)
self.base_url = base_url
self.matcher = matcher or cssselect2.Matcher()
self.page_rules = [] if page_rules is None else page_rules
self.fonts = []
preprocess_stylesheet(
media_type, base_url, stylesheet, url_fetcher, self.matcher,
self.page_rules, self.fonts, font_config)
class Attachment(object):
"""Represents a file attachment for a PDF document.
.. versionadded:: 0.22
An instance is created in the same way as :class:`HTML`, except that the
HTML specific arguments (``encoding`` and ``media_type``) are not
supported. An optional description can be provided with the ``description``
argument.
:param description: A description of the attachment to be included in the
PDF document. May be :obj:`None`.
"""
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
string=None, base_url=None, url_fetcher=default_url_fetcher,
description=None):
self.source = _select_source(
guess, filename, url, file_obj, string,
base_url=base_url, url_fetcher=url_fetcher)
self.description = description
@contextlib.contextmanager
def _select_source(guess=None, filename=None, url=None, file_obj=None,
string=None, base_url=None, url_fetcher=default_url_fetcher,
check_css_mime_type=False):
"""
Check that only one input is not None, and return it with the
normalized ``base_url``.
"""
if base_url is not None:
base_url = ensure_url(base_url)
selected_params = [
param for param in (guess, filename, url, file_obj, string) if
param is not None]
if len(selected_params) != 1:
raise TypeError('Expected exactly one source, got ' + (
', '.join(selected_params) or 'nothing'
))
elif guess is not None:
if hasattr(guess, 'read'):
type_ = 'file_obj'
elif isinstance(guess, Path):
type_ = 'filename'
elif url_is_absolute(guess):
type_ = 'url'
else:
type_ = 'filename'
result = _select_source(
base_url=base_url, url_fetcher=url_fetcher,
check_css_mime_type=check_css_mime_type,
**{type_: guess})
with result as result:
yield result
elif filename is not None:
if isinstance(filename, Path):
filename = str(filename)
if base_url is None:
base_url = path2url(filename)
with open(filename, 'rb') as file_obj:
yield 'file_obj', file_obj, base_url, None
elif url is not None:
with fetch(url_fetcher, url) as result:
if check_css_mime_type and result['mime_type'] != 'text/css':
LOGGER.error(
'Unsupported stylesheet type %s for %s',
result['mime_type'], result['redirected_url'])
yield 'string', '', base_url, None
else:
proto_encoding = result.get('encoding')
if base_url is None:
base_url = result.get('redirected_url', url)
if 'string' in result:
yield 'string', result['string'], base_url, proto_encoding
else:
yield (
'file_obj', result['file_obj'], base_url,
proto_encoding)
elif file_obj is not None:
if base_url is None:
# filesystem file-like objects have a 'name' attribute.
name = getattr(file_obj, 'name', None)
# Some streams have a .name like '', not a filename.
if name and not name.startswith('<'):
base_url = ensure_url(name)
yield 'file_obj', file_obj, base_url, None
elif string is not None:
yield 'string', string, base_url, None
else:
sources = dict(locals())
sources_names = ', '.join(
name for name in ('guess', 'filename', 'url', 'file_obj', 'string')
if sources[name] is not None) or 'nothing'
raise TypeError('Expected exactly one source, got ' + sources_names)
# Work around circular imports.
from .css import preprocess_stylesheet # noqa isort:skip
from .html import ( # noqa isort:skip
HTML5_UA_STYLESHEET, HTML5_PH_STYLESHEET, find_base_url, get_html_metadata)
from .document import Document, Page # noqa isort:skip
WeasyPrint-51/weasyprint/__main__.py 0000644 0001750 0001750 00000017206 13600156326 017645 0 ustar lize lize 0000000 0000000 """
weasyprint.__main__
-------------------
Command-line interface to WeasyPrint.
:copyright: Copyright 2011-2019 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import argparse
import logging
import os
import sys
import cairosvg
from . import HTML, LOGGER, VERSION
from .text import cairo, pango
class PrintInfo(argparse.Action):
def __call__(*_, **__):
uname = os.uname()
print('System:', uname.sysname)
print('Machine:', uname.machine)
print('Version:', uname.version)
print('Release:', uname.release)
print()
print('WeasyPrint version:', VERSION)
print('Python version:', sys.version.split()[0])
print('Cairo version:', cairo.cairo_version())
print('Pango version:', pango.pango_version())
print('CairoSVG version:', cairosvg.__version__)
sys.exit()
def main(argv=None, stdout=None, stdin=None):
"""The ``weasyprint`` program takes at least two arguments:
.. code-block:: sh
weasyprint [options]