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

The library ignores position:absolute styling #203

Open
zn opened this issue Jan 29, 2024 · 0 comments
Open

The library ignores position:absolute styling #203

zn opened this issue Jan 29, 2024 · 0 comments

Comments

@zn
Copy link

zn commented Jan 29, 2024

I have HTML code with many elements with style position: absolute. The HTML code looks good in the browser but when I try to convert it to pdf using HtmlRenderer.PdfSharp, I get all the elements in the top left corner. How can I fix this?

Here is my code:

using PdfSharp;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;

var html = "...";
File.WriteAllBytes("draft.html", Encoding.UTF8.GetBytes(html));
using PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4, margin:0);
using var ms = new MemoryStream();
pdf.Save(ms);
File.WriteAllBytes("draft.pdf", ms.ToArray());
return ms.ToArray();

The html:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    <p style="position: absolute; top: 50px; left: 200px;margin: 0;">Hello world!</p>
    <p style="position: absolute; top: 100px; left: 200px;margin: 0;">Hello world!1</p>
    <p style="position: absolute; top: 150px; left: 200px;margin: 0;">Hello world!2</p>
    <p style="position: absolute; top: 200px; left: 200px;margin: 0;">Hello world!3</p>
</body>
</html>

Below is how the generated pdf looks like:
MPt2s

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

1 participant