Skip to content

<IMG> width attribute behaviour seems wrong #4886

@wiz21b

Description

@wiz21b

Description of the bug

Hello,

I'm trying to generate a PDF from an HTML. I use the Story mechanism much like explained. My html code is like this (there's not styling):

<table>
    <tr>
        <td><img src="aplot.png" width="10"/></td>
        <td>ldkjf dsflksdf sdlfjdlsfjsdlfds fdlskfjdslf sd
            ldkjf dsflksdf sdlfjdlsfjsdlfds fdlskfjdslf s</td>
    </tr>
</table>

If I don't specify the <img...>'s width attribute, the image is rendered in the PDF with a size proportional to the original PNG. In particular, the size of the table's cell containing the image more or less fits the size of the PNG image, which looks fine to me. Now, if I set the width attribute of the IMG tag to something very small, the image is rendered in the PDF as a very small image (which is expected). However, the size of the table cell containing the image remains the same as when the PNG is not resized. I would have expected that the size of the cell would be adjusted to the size of the re-sized <img...>.

I've read that pymupdf doesn't render tables quite like a browser but I don't know if this issue is related to that.

Thanks, S.

How to reproduce the bug

Here's the code I use to generate the failure:

    # Create a Story object with HTML (no archive needed with data URIs)

    story = pymupdf.Story(html=html_content, archive=archive)
    # Define page dimensions (A4)
    mediabox = pymupdf.paper_rect("A4")
    where = mediabox + (36, 36, -36, -36)  # 36pt margins

    # Create a DocumentWriter
    pdf_in_mem = BytesIO()
    writer = pymupdf.DocumentWriter(pdf_in_mem)

    # Place content across multiple pages
    more = 1
    while more:
        # Begin a new page and get the device
        device = writer.begin_page(mediabox)

        # Place content on this page
        more, filled = story.place(where)

        # Draw the content
        story.draw(device)

        # End the page
        writer.end_page()

    # Close the writer
    writer.close()

    # Write to file
    pdf_in_mem.seek(0)
    pdf_bytes = pdf_in_mem.read()

    if to_pdf:
        with open(to_pdf, "wb") as pdf_out:
            pdf_out.write(pdf_bytes)
            print(f"Wrote {to_pdf}")

PyMuPDF version

1.26.7

Operating system

Windows

Python version

3.11

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions