将HTML页面转换为PDF时的视觉错误

发布于 2025-02-13 15:26:41 字数 2202 浏览 1 评论 0 原文

在PDF渲染网站时,我有一个视觉错误。正如您在屏幕截图上看到的那样,图像发生了变化。但是在HTML上渲染这种转变并不存在。

要转换的网站的链接: https://betterhost-3fadc.web.web.app/

<article>
<div class="container"><img
        src="https://www.electrodepot.fr/fstrz/r/s/www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P939881.jpg?frz-v=2690"><img
        src="https://www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P959338.jpg"><img
        src="https://www.alinea.com/fstrz/r/s/www.alinea.com/dw/image/v2/BCKM_PRD/on/demandware.static/-/Sites-ali_master/default/dwb3523209/images/27339299/POUBELLE-DAYA-27339299-F-1.jpg?sw=982&amp;sh=982&amp;sm=fit&amp;sfrm=png&amp;bgcolor=eef1eb&amp;frz-v=103"><img
        src="https://cdn.sklum.com/fr/1172047/distributeur-de-savon-en-verre-ovie.jpg"><img
        src="https://www.alinea.com/fstrz/r/s/www.alinea.com/dw/image/v2/BCKM_PRD/on/demandware.static/-/Sites-ali_master/default/dw32eea630/images/27258507/BROSSE-WC-SPINA-27258507-F-1.jpg?sw=982&amp;sh=982&amp;sm=fit&amp;sfrm=png&amp;bgcolor=eef1eb&amp;frz-v=103"><img
        src="https://www.electrodepot.fr/fstrz/r/s/www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P963865.jpg?frz-v=2690"><img
        src="https://cdn.sklum.com/fr/1172188/gobelet-en-verre-ovie.jpg"></div>
<cactus>

每个页面都在一个称为文章的块中。

article {
    position: relative;
    display: flex;
    width: 827px;
    height: 1170px;
    overflow: hidden;
    margin: 0px;
}

图像通过此CSS代码在2列中排列在2列中:

.container {columns: 2;width: 50%;column-gap: 0px;height: auto;}

web:

​href =“ https://i.sstatic.net/8adkc.png” rel =“ nofollow noreferrer”>

I have a visual bug when rendering my website in PDF. As you can see on the screen shot there is a shift in the images. But on the html rendering this shift is not present.

the link of the website to be transformed: https://betterhost-3fadc.web.app/

<article>
<div class="container"><img
        src="https://www.electrodepot.fr/fstrz/r/s/www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P939881.jpg?frz-v=2690"><img
        src="https://www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P959338.jpg"><img
        src="https://www.alinea.com/fstrz/r/s/www.alinea.com/dw/image/v2/BCKM_PRD/on/demandware.static/-/Sites-ali_master/default/dwb3523209/images/27339299/POUBELLE-DAYA-27339299-F-1.jpg?sw=982&sh=982&sm=fit&sfrm=png&bgcolor=eef1eb&frz-v=103"><img
        src="https://cdn.sklum.com/fr/1172047/distributeur-de-savon-en-verre-ovie.jpg"><img
        src="https://www.alinea.com/fstrz/r/s/www.alinea.com/dw/image/v2/BCKM_PRD/on/demandware.static/-/Sites-ali_master/default/dw32eea630/images/27258507/BROSSE-WC-SPINA-27258507-F-1.jpg?sw=982&sh=982&sm=fit&sfrm=png&bgcolor=eef1eb&frz-v=103"><img
        src="https://www.electrodepot.fr/fstrz/r/s/www.electrodepot.fr/media/catalog/product/cache/1a40d1f945549a9ec18309b0a600e55c/P963865.jpg?frz-v=2690"><img
        src="https://cdn.sklum.com/fr/1172188/gobelet-en-verre-ovie.jpg"></div>
<cactus>

Each page is in a block called article.

article {
    position: relative;
    display: flex;
    width: 827px;
    height: 1170px;
    overflow: hidden;
    margin: 0px;
}

The images are arranged in 2 columns via this CSS code:

.container {columns: 2;width: 50%;column-gap: 0px;height: auto;}

Web:

Code render web

PDF:

PDF RENDER

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2025-02-20 15:26:41

您需要专用的打印样式表来制作可靠的PDF。使用媒体查询制作样式表进行打印。在您的标记中,在一个称为.page的容器中,将所有a4的所有内容都包含在一张a4中。然后在您的打印样式表中,使用@page探索以格式化PDF。

在print.css中

.page {
   page-break-inside: avoid;
}

,应该在类=“ page”容器中停止布局的布局在两个页面上破裂。尝试在打印样式表中使用真实尺寸,以使所有内容都适合您所需的一页。您可以通过执行CTRL+P来查看打印预览,或者实际上从Chrome中保存到PDF来查看PDF的外观。

那应该从正确的方向开始。

编辑:其他一些指针

将其删除默认利润率,当打印

@page 
{
    size: auto;   /* auto is the current printer page size */
    margin: 0mm;  /* this affects the margin in the printer settings */
}

将页面容器设置为几乎与要打印的页面大小

.page {
width:209mm;height:296mm;background-color:#fffffe;position:relative;border:1px solid transparent
}

You need a dedicated print style sheet to make reliable PDFs. use a media query to make a style sheet for print. In your markup have everything for one sheet of a4 in a container called something like .page. Then in your print style sheet, explore using @page to format the pdf.

in print.css

.page {
   page-break-inside: avoid;
}

That should stop your layout within your class="page" container from breaking across two pages. Experiment with using real dimensions in your print style sheet to make everything fit on one page as you want. You can see what your pdf is going to look like by doing ctrl+p to see the print preview, or actually saving to pdf from chrome.

That should start you in the right direction.

EDIT: some other pointers

put this to take the default margins off when printing

@page 
{
    size: auto;   /* auto is the current printer page size */
    margin: 0mm;  /* this affects the margin in the printer settings */
}

Set your page container to nearly the size of the page you want to print

.page {
width:209mm;height:296mm;background-color:#fffffe;position:relative;border:1px solid transparent
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文