Skip to content

Commit

Permalink
Merge pull request #815 from SebCorbin/patch-1
Browse files Browse the repository at this point in the history
Fix #814 Documentation about custom url_fetcher
  • Loading branch information
liZe authored Feb 28, 2019
2 parents aacf769 + 97476aa commit c294a65
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,37 @@ to the default fetcher:
graph_data = map(float, url[6:].split(','))
return dict(string=generate_graph(graph_data),
mime_type='image/png')
else:
return weasyprint.default_url_fetcher(url)
return weasyprint.default_url_fetcher(url)
source = '<img src="graph:42,10.3,87">'
HTML(string=source, url_fetcher=my_fetcher).write_pdf('out.pdf')
Flask-WeasyPrint_ makes use of a custom URL fetcher to integrate WeasyPrint
with a Flask_ application and short-cut the network for resources that are
within the same application.
Flask-WeasyPrint_ for Flask_ and Django-Weasyprint_ for Django both make
use of a custom URL fetcher to integrate WeasyPrint and use the filesystem
instead of a network call for static and media files.

A custom fetcher should be returning a :obj:`dict` with

* One of ``string`` (a :obj:`bytestring <bytes>`) or ``file_obj``
(a :term:`file object`).
* Optionally: ``mime_type``, a MIME type extracted e.g. from a
*Content-Type* header. If not provided, the type is guessed from the
file extension in the URL.
* Optionally: ``encoding``, a character encoding extracted e.g. from a
*charset* parameter in a *Content-Type* header
* Optionally: ``redirected_url``, the actual URL of the resource
if there were e.g. HTTP redirects.
* Optionally: ``filename``, the filename of the resource. Usually
derived from the *filename* parameter in a *Content-Disposition*
header

If a ``file_obj`` is given, the resource will be closed automatically by
:func:`~weasyprint.urls.fetch`.

.. _Flask-WeasyPrint: http://packages.python.org/Flask-WeasyPrint/
.. _Flask: http://flask.pocoo.org/
.. _Django-WeasyPrint: https://pypi.org/project/django-weasyprint/
.. _Django: https://www.djangoproject.com/


Logging
Expand Down

0 comments on commit c294a65

Please sign in to comment.