Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unignoreable 404 Error for non existing images in IMG tag #250

Closed
cinatic opened this issue May 3, 2015 · 6 comments
Closed

Unignoreable 404 Error for non existing images in IMG tag #250

cinatic opened this issue May 3, 2015 · 6 comments

Comments

@cinatic
Copy link
Contributor

cinatic commented May 3, 2015

Hello,

i get an exception that a image has not been found. In my opinion it should be possible to render the pdf even if an image could not be loaded, e.g. by a parameter.

Version: python-weasyprint 0.23

  File "/usr/lib/python3.4/site-packages/weasyprint/__init__.py", line 164, in write_pdf
    return self.render(stylesheets).write_pdf(target, zoom, attachments)
  File "/usr/lib/python3.4/site-packages/weasyprint/document.py", line 487, in write_pdf
    self.url_fetcher)
  File "/usr/lib/python3.4/site-packages/weasyprint/pdf.py", line 627, in write_pdf_metadata
    annot_files = _write_pdf_annotation_files(pdf, links, url_fetcher)
  File "/usr/lib/python3.4/site-packages/weasyprint/pdf.py", line 575, in _write_pdf_annotation_files
    pdf, (target, None), url_fetcher)
  File "/usr/lib/python3.4/site-packages/weasyprint/pdf.py", line 542, in _write_pdf_attachment
    with attachment.source as (source_type, source, url, _):
  File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.4/site-packages/weasyprint/__init__.py", line 297, in _select_source
    with fetch(url_fetcher, url) as result:
  File "/usr/lib/python3.4/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.4/site-packages/weasyprint/urls.py", line 315, in fetch
    raise URLFetchingError('%s: %s' % (name, value) if value else name)
weasyprint.urls.URLFetchingError: HTTPError: HTTP Error 404: Not Found

Best regards
Florijan

@cinatic
Copy link
Contributor Author

cinatic commented May 3, 2015

Catching Exceptions and adding null checks up the tree works just fine. Patch is comming

@SimonSapin
Copy link
Member

A test case to reproduce the issue would be good too. Thanks!

@cinatic
Copy link
Contributor Author

cinatic commented May 3, 2015

Very Simple

html = """<html><body><img src="/i/do/not/exist.png"/></body></html>"""

t = HTML(string=html, base_url="your domain")
data = t.write_pdf()

@SimonSapin
Copy link
Member

I can’t reproduce this in WeasyPrint master. This test case produces a warning:

Failed to load image at file:///i/do/not/exist.png : URLError: <urlopen error [Errno 2] No such file or directory: '/i/do/not/exist.png'>

… but no exception, and t.write_pdf() does return a byte string with PDF data.

What’s strange in the traceback you give above is that WeasyPrint is apparently trying to write a PDF attachment, while an image alone should not be creating an attachment in the first place.

@cinatic
Copy link
Contributor Author

cinatic commented May 4, 2015

Yes you are right and this is also the difference:

from weasyprint import HTML, CSS

html = """<html><body><a href="/i/do/not/exist.png" rel="attachment"><img src="/i/do/not/exist.png"/></a></body></html>"""

t = HTML(string=html, base_url="https://www.google.de/")
data = t.write_pdf()

i forgot that there is a link sorry for my inacurate example

@liZe liZe closed this as completed in f0da037 Aug 14, 2015
@liZe
Copy link
Member

liZe commented Aug 14, 2015

The try block can probably can be smaller, but I'm not sure.

jsonn referenced this issue in jsonn/pkgsrc Jan 8, 2016
Version 0.25
------------

Released on 2015-12-17.

New features:

* Support the 'q' unit.

Bug fixes:

* `#285: <https://github.com/Kozea/WeasyPrint/issues/285>`_:
  Fix a crash happening when splitting lines.
* `#284: <https://github.com/Kozea/WeasyPrint/issues/284>`_:
  Escape parenthesis in PDF links.
* `#280: <https://github.com/Kozea/WeasyPrint/pull/280>`_:
  Replace utf8 with utf-8 for gettext/django compatibility.
* `#269: <https://github.com/Kozea/WeasyPrint/pull/269>`_:
  Add support for use when frozen.
* `#250: <https://github.com/Kozea/WeasyPrint/issues/250>`_:
  Don't crash when attachments are not available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants