kiklop74 / moodle-local_dompdf
Moodle 的 DomPDF 库
v1.5
2024-07-17 16:04 UTC
Requires
- php: ^7.2||^8.0
- ext-dom: *
- ext-gd: *
- ext-mbstring: *
- composer/installers: ^2.0
Requires (Dev)
- dompdf/dompdf: v3.0.0
Suggests
- ext-gmagick: Image processing optimization.
- ext-imagick: Image processing optimization.
README
Dompdf 是一个 HTML 转 PDF 的转换器
在核心上,dompdf 是(主要)一个符合 CSS 2.1 的 HTML 布局和渲染引擎,用 PHP 编写。它是一个基于样式的渲染器:它会下载和读取外部样式表、内联样式标签以及单个 HTML 元素的样式属性。它还支持大多数表现性 HTML 属性。
需求
- PHP 7.1+
- Moodle 3.5+
- MBString 扩展
推荐
- OPcache(OPcache、XCache、APC 等):提高性能
- IMagick 或 GMagick 扩展:提高图像处理性能
更多信息请访问 Wiki: https://github.com/dompdf/dompdf/wiki/Requirements
内部设置
默认情况下,每当生成 PDF 时,系统都会使用 $CFG->localcachedir/dompdf
存储临时数据。
使用方法
要创建 PDF 类的新实例,请使用以下代码
$pdf = \local_dompdf\api\pdf::createnew();
这为您提供了一个 \Dompdf\Dompdf
类的实例,您只需按照官方 库文档 中的说明使用它即可。
存储在 Moodle HTML 编辑器中的图像。
如果您想将包含来自 Moodle 内部文件系统的图像的 HTML 转换为 PDF,您需要为每个特定字段执行图像编码。
例如,如果您有一个可以在 HTML 编辑器中设置图像的位置,当您从数据库检索它以在屏幕上显示时,您将使用此代码
$rawtext = $DB->get_field('sometable', 'somefield', ['id' => 123]);
$options = [
'noclean' => true, 'para' => false, 'filter' => true,
'context' => $context, 'overflowdiv' => true
];
$intro = file_rewrite_pluginfile_urls(
$rawtext, 'pluginfile.php', $context->id, $component, $filearea, $itemid
);
$value = format_text($intro, $format, $options, null);
对于 Dmpdf,这不起作用。有一个特定的重写方法,它将图像直接编码到 HTML 中,并使它们可以通过库使用
$rawtext = $DB->get_field('sometable', 'somefield', ['id' => 123]);
$options = [
'noclean' => true, 'para' => false, 'filter' => true,
'context' => $context, 'overflowdiv' => true
];
$intro = \local_dompdf\api\pdf::file_rewrite_image_urls(
$rawtext, $itemid, $filearea, $contextid, $component
);
$value = format_text($intro, $format, $options, null);
示例
示例位于插件的 examples 目录中。