def test_low_level_api(assert_pixels_equal):
    html = FakeHTML(string='<body>')
    css = CSS(string='''
        @page { margin: 2px; size: 8px }
        html { background: #00f; }
        body { background: #f00; width: 1px; height: 1px }
    ''')
    pdf_bytes = html.write_pdf(stylesheets=[css])
    assert pdf_bytes.startswith(b'%PDF')

    png_bytes = html.write_png(stylesheets=[css])
    document = html.render(stylesheets=[css])
    page, = document.pages
    assert page.width == 8
    assert page.height == 8
    assert document.write_png() == png_bytes
    assert document.copy([page]).write_png() == png_bytes

    document = html.render(stylesheets=[css])
    page, = document.pages
    assert (page.width, page.height) == (8, 8)
    png_bytes = document.write_png(resolution=192)
    check_png_pattern(assert_pixels_equal, png_bytes, x2=True)

    document = html.render(stylesheets=[css])
    page, = document.pages
    assert (page.width, page.height) == (8, 8)
    # A resolution that is not multiple of 96:
    assert _png_size(document.write_png(resolution=145.2)) == (12, 12)

    document = FakeHTML(string='''
        <style>
            @page:first { size: 5px 10px } @page { size: 6px 4px }
            p { page-break-before: always }
        </style>
        <p></p>
        <p></p>
    ''').render()
    page_1, page_2 = document.pages
    assert (page_1.width, page_1.height) == (5, 10)
    assert (page_2.width, page_2.height) == (6, 4)

    result = document.write_png()
    # (Max of both widths, Sum of both heights)
    assert _png_size(result) == (6, 14)
    assert document.copy([page_1, page_2]).write_png() == result
    assert _png_size(document.copy([page_1]).write_png()) == (5, 10)
    assert _png_size(document.copy([page_2]).write_png()) == (6, 4)
)
pdf_bytes = html.write_pdf(stylesheets=[css])
assert pdf_bytes.startswith(b'%PDF')

png_bytes = html.write_png(stylesheets=[css])
document = html.render(stylesheets=[css])
page, = document.pages
assert page.width == 8
assert page.height == 8
assert document.write_png() == png_bytes
assert document.copy([page]).write_png() == png_bytes

document = html.render(stylesheets=[css])
page, = document.pages
assert (page.width, page.height) == (8, 8)
png_bytes = document.write_png(resolution=192)
check_png_pattern(assert_pixels_equal, png_bytes, x2=True)

document = html.render(stylesheets=[css])
page, = document.pages
assert (page.width, page.height) == (8, 8)
# A resolution that is not multiple of 96:
assert _png_size(document.write_png(resolution=145.2)) == (12, 12)

document = FakeHTML(string=
tests/test_api.py