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

Error in write_pdf after Document.copy #880

Closed
ewingrj opened this issue Jun 13, 2019 · 6 comments
Closed

Error in write_pdf after Document.copy #880

ewingrj opened this issue Jun 13, 2019 · 6 comments
Labels
crash Problems preventing documents from being rendered
Milestone

Comments

@ewingrj
Copy link

ewingrj commented Jun 13, 2019

I am getting the following error when combining multiple pages into a single document similar to how the example shows:

                combined_invoice = tempfile.SpooledTemporaryFile()
                all_pages = [p for p in doc.pages for doc in documents]
                documents[0].copy(all_pages).write_pdf(combined_invoice)

error:

Traceback (most recent call last):
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/contrib/admin/options.py", line 552, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 224, in inner
    return view(request, *args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1606, in changelist_view
    response = self.response_action(request, queryset=cl.get_queryset(request))
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1287, in response_action
    response = func(self, request, queryset)
  File "/Users/rjewing/code/personal/ffcsa/ffcsa/core/admin.py", line 166, in download_invoices
    documents[0].copy(all_pages).write_pdf(combined_invoice)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/document.py", line 602, in write_pdf
    page.paint(context, scale=scale)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/document.py", line 270, in paint
    draw_page(self._page_box, cairo_context, self._enable_hinting)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/draw.py", line 156, in draw_page
    stacking_context = StackingContext.from_page(page)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 56, in from_page
    child_contexts = [cls.from_box(child, page) for child in page.children]
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 56, in <listcomp>
    child_contexts = [cls.from_box(child, page) for child in page.children]
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 143, in from_box
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 121, in dispatch
    box = dispatch_children(box)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 137, in dispatch_children
    result = dispatch(child)
  File "/Users/rjewing/.pyenv/versions/ffcsa/lib/python3.5/site-packages/weasyprint/stacking.py", line 79, in dispatch
    style = box.style
AttributeError: 'StackingContext' object has no attribute 'style'
@liZe
Copy link
Member

liZe commented Jun 22, 2019

Hello!

Thank you for this report. Is it possible for you to give a specific example? It's hard to reproduce without a simple snippet.

@liZe
Copy link
Member

liZe commented Sep 13, 2019

Feel free to reopen the issue if needed.

@Tontyna
Copy link
Contributor

Tontyna commented May 28, 2020

Please reopen!

Snippet to crash WeasyPrint:

    myhtml = HTML(string='<input type="checkbox">')
    doc = myhtml.render()
    doc.write_pdf('output1.pdf')
    doc.write_pdf('output2.pdf')

@liZe liZe reopened this May 28, 2020
@liZe liZe added the crash Problems preventing documents from being rendered label Jun 22, 2020
@smeggingsmegger
Copy link

<style>
  footer {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 2.5cm;
    width: 100%;
    padding: 0 84px;
    background-color: #00e300;
  }
  
footer p {
    color: red;
    font-size: 13px;
    font-family: hero-new;
  }
.footer-wrap {
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
}
</style>
<footer>
  <div class="footer-wrap">
    <p>Page 1 of 1   >   © COMPANY.COM   >   [email protected]</p>
  </div>
</footer>

Will crash with this error: "'StackingContext' object has no attribute 'style'"

display: inline-block; inside of .footer-wrap is the cause of the crash. Remove that item and it stops crashing.

@liZe
Copy link
Member

liZe commented Jul 13, 2021

Hello @smeggingsmegger

Will crash with this error: "'StackingContext' object has no attribute 'style'"

That’s just when Document.copy is called, right? I can’t reproduce with a "normal" rendering, so I think that we can close #1396.

@liZe
Copy link
Member

liZe commented Jul 13, 2021

The problem comes from cfda2f1, that’s been written in 2012!

When we have inline blocks (and now inline flex boxes), we insert a fake stacking context in box children. Modifying the tree is definitely a bad idea (just as modifying the style dictionary was), but we won’t change that right now.

Note for the future: don’t modify the tree.

Let’s just take the box in the stacking context when we meet one, that should be enough.

@liZe liZe added this to the 53.0 milestone Jul 13, 2021
@liZe liZe closed this as completed in 1111f7d Jul 13, 2021
liZe added a commit that referenced this issue Apr 15, 2022
The bug #880 has been resolved by handling stacking contexts stored as box
children, adding this condition.

But the stacking contexts stored as box children introduced other bugs. #1473
was another bug, and its fix also removed the problem of #880.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Problems preventing documents from being rendered
Projects
None yet
Development

No branches or pull requests

4 participants