标签: weasyprint

在 pdf weasyprint 中附加 img 文件

我需要帮助在 pdf 中附加 img 文件。我们使用 Wea​​syPrint lib 从 html 生成 pdf。

在html中像这样连接img文件

<img src="1.png" alt="">
<img src="2.png" alt="">
<img src="3.png" alt="">
Run Code Online (Sandbox Code Playgroud)

但它不起作用。我没有看到图像。

python django pdf-generation weasyprint

8
推荐指数
1
解决办法
8467
查看次数

Weasyprint 在调用 write_pdf 时获得未定义的属性:“AttributeError:‘PosixPath’对象没有属性‘read_text’”

我正在 ubuntu 18.04 上运行 weasyprint 项目,并尝试创建 aa pdf。

当我尝试设置页脚图像时,问题就开始了。我正在 python 3.6.7 上运行

这是我调用 weasyprint 的代码:

import sys
import os
from weasyprint import HTML, CSS

htmlFile = sys.argv[1]
pdfFile = sys.argv[2]

html = HTML(filename=htmlFile)

css = CSS(string='@page { @bottom-center { width: 125%; margin-top: 10px; content: " "; background-image: url(file://' + os.getcwd() + '/pdf/footer.png); background-repeat: no-repeat; background-position: right;  background-size: 100%; } }')

html.write_pdf(pdfFile, stylesheets=[css])
Run Code Online (Sandbox Code Playgroud)

但后来我收到以下错误:

Error: Command failed: python3 /node_modules/my-project/python/run.py /node_modules/my-project/pdf/catalog_01e299a3-1fca-482b-a512-e1bea832559f.html /node_modules/my-project/pdf/catalog_01e299a3-1fca-482b-a512-e1bea832559f.pdf
Traceback (most recent call last):
File "/node_modules/my-project/python/run.py", line 3, in …
Run Code Online (Sandbox Code Playgroud)

python cairo weasyprint ubuntu-18.04

8
推荐指数
1
解决办法
1万
查看次数

cairocffi 在 Ubuntu 18.04 上安装失败

我正在pip freeze一个 python 项目上运行,它显示除数字版本之外的字符串 VERSION 并且包安装失败,警告消息是:

weasyprint 43 有要求 cairocffi>=0.9.0,但您将拥有不兼容的 cairocffi file-.cairocffi-VERSION。

cairocffi===file-.cairocffi-VERSION
Run Code Online (Sandbox Code Playgroud)

cairoccffi 是我在项目中使用的 Weasyprint 的依赖项,并且 weasypring 版本是正确的:

WeasyPrint==43
Run Code Online (Sandbox Code Playgroud)

我尝试使用pipreqs生成 requirements.txt,它没有显示 cairocffi 包,但包安装失败并出现相同的错误。

我检查了 site-packages 文件夹,它包含

cairocffi-file_.cairocffi_VERSION.dist-info/
Run Code Online (Sandbox Code Playgroud)

导致问题的原因。

有人可以帮忙吗?

python-3.x requirements.txt weasyprint

8
推荐指数
1
解决办法
1947
查看次数

在Windows上获取"OSError:dlopen()无法加载库:cairo/cairo-2"

OSError: dlopen() failed to load a library: cairo / cairo-2在尝试在新安装后执行Django时得到了.使用Windows.

我正在使用Weasyprint,似乎与那个相关,根据完整的痕迹:

Unhandled exception in thread started by <function wrapper at 0x03F605B0>
Traceback (most recent call last):
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\my-venv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "C:\my-venv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\my-venv\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\my-venv\lib\site-packages\django\apps\registry.py", line 115, in populate
    app_config.ready()
  File "C:\my-venv\lib\site-packages\debug_toolbar\apps.py", line 15, in ready
    dt_settings.patch_all()
  File …
Run Code Online (Sandbox Code Playgroud)

django weasyprint

7
推荐指数
2
解决办法
7294
查看次数

分页后重新显示当前标题

我正在使用 Wea​​syPrint 创建一个文档。我有一些有名字的部分,其中一些可能跨越多个页面。当一个部分太长时,会发生分页符。我想要做的是重新显示当前部分的名称,最好使用相同的格式。

以下 MWE 显示了分页符后如何不显示部分标题:

<html>
    <body>
        <h1>First section</h1>
        <p>Lorem ipsum...</p>
        <p>Lorem ipsum...</p>
        <p>Lorem ipsum...</p>
        <p>Lorem ipsum...</p>

        <p style="break-after: always;"></p>

        <p>Lorem ipsum...</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

的输出weasyprint example.html example.pdf

在此处输入图片说明

我想First section作为<h1>标签显示在左侧页面的顶部。

我想做这个 tex.stackexchange帖子,据我所知,它基本上包括检查当前页码是否超过当前总页数,如果超过,插入遇到的最后一节标题。

我不知道在 HTML 中这样做的可能性,它存在吗?有什么解决方法可以做到这一点吗?如果没有,是否可以让 WeasyPrint 在某个page-break钩子上执行自定义 Python 代码?

html css python weasyprint

7
推荐指数
1
解决办法
1816
查看次数

根据 CSS 规则测量文本高度 - _无需浏览器渲染_ - 用于虚拟化列表,提前指定高度

我一直在用 Electron (Chrome) 和 React 实现一个聊天客户端。我们的首要任务是速度。因此,我们应该使用虚拟化列表组件(也称为“缓冲渲染”或“窗口渲染”)。我们已经探索了 react-virtualized、react-window 和 react-infinite 等。

所有这些组件的一个共同点是,如果支持可变高度的列表元素,则需要提前知道高度。现在,有些聊天很长,有些很短,这对我们来说是一个挑战。(由于 EXIF 数据和 ffprobe,图像和视频很容易)。

因此,我们面临着测量高度的挑战,同时也竭力追求卓越的性能。一种明显的技术是将元素放在浏览器容器中,离开视口,执行测量,然后呈现列表。但这在性能要求方面伤害了我们。像 react-virtualized/CellMeasurer(不再由原作者维护)和 react-window 之类的软件使我们采用了这种技术,内置于库中,但性能有点慢且不可靠。一个可能更高效的类似想法是使用后台电子浏览器窗口来进行渲染和测量,但我的直觉是这不会更快。

我认为必须有一些解决的方法可以根据自动换行、最大宽度和字体规则提前确定字符串高度。

我目前的想法是使用像string-pixel-width这样的库,以便在我们通过 API 获取文本数据后立即计算行高。基本上,该库使用这段代码来生成字符宽度 [*] 的映射。然后,一旦我们知道每个文本的宽度,当它最大化计算出的最大行宽时,我们将每行分开,最后通过行数推断列表元素的高度。由于断字,它需要一点点算法摆弄,但有库可以帮助解决这个问题 - css-line-break似乎很有希望。

[*] 我们将不得不稍微修改它以考虑所有 Unicode 字符范围,但这是微不足道的。

我还没有完全探索的一些选项包括 python weasyprint 项目和 facebook-yoga 项目。我愿意接受你的想法!

weasyprint electron react-virtualized react-infinite react-window

7
推荐指数
1
解决办法
260
查看次数

静默运行 WeasyPrint 脚本

WeasyPrint 不支持某些 CSS3 功能(我希望如此),并且运行 Python 脚本会抛出一长串被忽略的条目(例如框阴影、渐变),然后是作业摘要。是否可以在静默模式下运行 WeasyPrint,而不显示任何信息?

python console weasyprint

6
推荐指数
2
解决办法
1623
查看次数

如何将weasyprint生成的PDF文件保存(存储)到指定目录?

我需要将weasyprint生成的pdf文件作为某个指定的文件夹,例如在文件夹“ my_project/pdf_files/ ”中

注意:我正在为我的项目使用 django 框架。这是项目的结构。

my_project/

----pdf_generator_app/

--------admin.py

--------models.py

--------views.py

pdf_files/
Run Code Online (Sandbox Code Playgroud)

目前我在我的views.py 中有这个

def generate(student_id):
    student = get_object_or_404(Student, application_id=student_id)
    html = render_to_string('contract.html', {'student': student})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="some_file.pdf"'
    weasyprint.HTML(string=html).write_pdf('myfile.pdf', 
                                           presentational_hints=True,
                                           stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + '/css/styles.css')])
    return response
Run Code Online (Sandbox Code Playgroud)

但是此视图不会将 pdf 文件存储到目录中。它只是在浏览器上显示文件。我需要将文件存储在目录my_project/pdf_files/

python django weasyprint

6
推荐指数
1
解决办法
5356
查看次数

如何从PDF中删除边距?(使用 Wea​​syPrint 生成)

我正在尝试在 Flask 应用程序中呈现 PDF 文档。为此,我使用以下 HTML 模板:

<!DOCTYPE html>

<html>
<head>
<style>
    @page {
        margin:0
    }
    h1 {
        color:white;
    }
    .header{
        background: #0a0045;
        height: 250px;
    }
    .center {
        position: relative;
        top: 50%;
        left: 50%;
        -ms-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        text-align:center;
    }

</style>
</head>
<body>
<div class="header">
    <div class="center">
        <h1>Name</h1>
    </div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的标题部分的顶部和右侧/左侧不断出现白色边距:

如何从该 PDF 中删除边距?

有没有办法去除它们?

编辑:

下面是用于在我的 Flask 应用程序中使用 Wea​​syPrint 生成 PDF 文件的代码:

def generate_pdf(id):
    element = Element.query.filter_by(id=id).first()
    attri_dict = get_element_attri_dict_for_tpl(element)
    html = render_template('element.html', attri_dict=attri_dict)
    pdf = HTML(string=html).write_pdf()
    destin_loc …
Run Code Online (Sandbox Code Playgroud)

python pdf margin flask weasyprint

6
推荐指数
1
解决办法
3234
查看次数

无法加载库 libcairo

尝试在 Django 中运行网站时遇到问题:

OSError: no library called "libcairo-2" was found
cannot load library 'libcairo.so.2': /lib/x86_64-linux-gnu/libfontconfig.so.1: undefined symbol: FT_Done_MM_Var
cannot load library 'libcairo.so': /lib/x86_64-linux-gnu/libfontconfig.so.1: undefined symbol: FT_Done_MM_Var
cannot load library 'libcairo.2.dylib': libcairo.2.dylib: cannot open shared object file: No such file or directory
cannot load library 'libcairo-2.dll': libcairo-2.dll: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)

虽然安装了包。

我已经安装了 weasyprint

pip3 install weasyprint

python -m pip install WeasyPrint




  sudo apt-get install build-essential python3-dev python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 …
Run Code Online (Sandbox Code Playgroud)

python django cairo weasyprint

6
推荐指数
1
解决办法
5577
查看次数