Skip to content

refactor(htmltopdf): remove QtWebEngine dependency - #449

Merged
deepin-bot[bot] merged 4 commits into
linuxdeepin:masterfrom
add-uos:task-v8179-refactor-htmltopdf-remove-webengine
Nov 23, 2026
Merged

deepin-bot[bot] merged 4 commits into
linuxdeepin:masterfrom
add-uos:task-v8179-refactor-htmltopdf-remove-webengine

Conversation

@add-uos

@add-uos add-uos commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Port of the htmltopdf QtWebEngine removal to current master (originally developed against 6.0.11.1 / qmake+Qt5.11.3). htmltopdf no longer links QtWebEngine at all: HTML is rendered with QTextDocument and printed to vector PDF via QPrinter.

Changes

  • htmltopdfconverter.h/.cpp: replace QWebEnginePage with QTextDocument + QPrinter (A4, 10mm page margins, fonts embedded automatically by the PDF engine).
    • Custom image handler separates display size from embedded resolution: images without explicit size are clamped to the text area width (no more clipped oversized screenshots), while the embedded bitmap keeps the original resolution up to a 300dpi cap (controlled PDF size, sharp printing/zooming).
    • Handler sizes are scaled by the print paint device DPI (same reference as Qt's default QTextImageHandler), keeping the physical image size consistent with text at any printer DPI.
    • Relative resources (images) resolve against the HTML file's directory, matching browser behavior.
  • main.cpp: drop the WebEngine crash workarounds (Wayland --disable-gpu, sw_64 --js-flags=--jitless) — no longer applicable.
  • htmltopdf.pro / CMakeLists.txt: link PrintSupport instead of WebEngineWidgets.
  • reader qmake sources (deepin_reader.pro, reader.pro, src.pri): drop the unused webenginewidgets module (the main program contains no WebEngine code).
  • debian/control: remove qt6-webengine-dev (Build-Depends) and libqt6webenginecore6-bin (Depends).
  • linglong.yaml: runtime org.deepin.runtime.webengine → org.deepin.runtime.dtk/6.7.0.

The CLI is unchanged (htmltopdf <input.html> <output.pdf>); the main program still launches htmltopdf as a subprocess, so no reader-side changes are needed.

Verification

  • htmltopdf builds via CMake (Qt 6.8) and ldd shows no WebEngine/Chromium libraries.
  • Functional test (Chinese text, headings, tables, lists, bold/italic, relative-path image): text renders and is extractable from the PDF; image fills the text area with correct aspect ratio and is embedded at ~268ppi (no quality loss vs. the old WebEngine screenshot pipeline).
  • 3000×2000 screenshot test: embedded bitmap downscaled to 2243px (300dpi cap) as designed; display size unaffected.
  • Explicit width/height attributes render at the given CSS size (missing dimension completed from the image aspect ratio, matching browser semantics).
  • mipsel/mips64el no longer need to be excluded: the [!mipsel !mips64el] guards on the removed WebEngine entries are gone with them.

Summary by Sourcery

Remove QtWebEngine from HTML-to-PDF conversion and project packaging while retaining compatible PDF output with improved image quality and portability.

New Features:

  • Replace QtWebEngine-based HTML-to-PDF conversion with QTextDocument and QPrinter while preserving the existing command-line interface.
  • Improve image handling with relative-resource resolution, display-size clamping, explicit dimension support, and resolution-controlled PDF embedding.

Bug Fixes:

  • Remove WebEngine-specific runtime workarounds and dependencies, including platform crash flags and WebEngine package requirements.

Enhancements:

  • Update qmake and CMake configurations to use PrintSupport and remove unused WebEngine modules from the reader.
  • Switch the Linglong runtime to the DTK runtime and allow builds on platforms previously excluded by WebEngine requirements.

Deployment:

  • Replace the WebEngine-based Linglong runtime with the DTK runtime.

Tests:

  • Add comprehensive htmltopdf unit coverage for PDF generation, image sizing and embedding, DPI scaling, resource preloading, and error handling.
  • Extend test coverage collection to include htmltopdf and stabilize test-process coverage output and exit handling.

Replace the QWebEnginePage-based HTML-to-PDF conversion with
QTextDocument + QPrinter, so htmltopdf no longer links WebEngine at all:

- htmltopdfconverter: render HTML with QTextDocument and print it to
  vector PDF via QPrinter (A4, fonts embedded automatically); add a
  custom image handler that separates display size from embedded
  resolution (oversized screenshots are clamped to the text area and
  embedded up to 300dpi) and scale handler sizes by the print paint
  device DPI to keep physical size consistent with text
- main.cpp: drop WebEngine crash workarounds (Wayland --disable-gpu,
  sw_64 --js-flags=--jitless), no longer needed
- build: htmltopdf.pro/CMakeLists.txt link PrintSupport instead of
  WebEngineWidgets; drop unused webenginewidgets from the reader
  qmake sources (no WebEngine code in the main program)
- debian/control: remove qt6-webengine-dev and libqt6webenginecore6-bin
- linglong.yaml: use org.deepin.runtime.dtk instead of the webengine
  runtime

The command-line interface is unchanged; Model.cpp still launches
htmltopdf as a subprocess.
@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 23 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Replace the converter’s WebEngine page member and completion slots with the QObject-based QTextDocument/QPrinter converter interface.

  • htmltopdf/htmltopdfconverter.h L2-30 (5 small edits)

Uses of the API

Keep the htmltopdf command-line entry point intact while removing the WebEngine-specific startup workarounds.

  • htmltopdf/main.cpp L10-47 (4 small edits)

Implementation

Implement HTML-to-PDF rendering with QTextDocument and QPrinter, including DPI-aware image sizing, capped embedding resolution, and resource preloading for print clones.

  • htmltopdf/htmltopdfconverter.cpp L8-283

Configuration

Replace WebEngine build and runtime dependencies with PrintSupport or the DTK runtime, and remove unused WebEngine modules and package dependencies.

  • deepin_reader.pro L8, L21
  • htmltopdf/CMakeLists.txt L19, L26
  • htmltopdf/htmltopdf.pro L6
  • linglong.yaml L16
  • reader/reader.pro L7, L16
  • reader/src.pri L38, L47
  • debian/control L15 (removed lines), L43 (removed lines)

Configure the test target’s PrintSupport dependencies and moc generation so it can compile the converter’s white-box tests.

  • tests/CMakeLists.txt L160-163, L255-308 (+3 small edits)

Tests

Add white-box coverage for PDF conversion, image sizing and embedding, DPI scaling, and preloaded resources surviving document cloning.

  • tests/htmltopdf/ut_htmltopdfconverter.cpp L1-434

Keep the application alive for printer-based tests and explicitly flush coverage before bypassing unstable third-party teardown.

  • tests/main.cpp L21-28, L50-54 (+4 small edits)

Recognize the observed third-party teardown crashes and include htmltopdf sources in coverage collection.

  • tests/test-prj-running.sh L68-73, L79-84, L94-95

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

QTextDocument::print() lays out and draws on an internal clone of the
document, and QTextDocument::clone() copies neither the baseUrl nor the
cachedResources of the original. The override loadResource() of
EmbeddingTextDocument (the 300-dpi embedding cap) therefore never runs
for the clone; print()'s rendering fell back to the resource loader's
relative-path resolution, which depends on the current working
directory and silently drops every image when the cwd is not the HTML
directory. Worse, the first layout pass caches the raw QPixmap in
cachedResources, so resource()-based preload stored the unscaled image
and the cap never took effect.

Fix preloadImageResources() to:

- call loadResource() directly (bypassing the resource() cache hit) via
  a new EmbeddingTextDocument::loadScaledResource() wrapper, since the
  virtual is protected and cannot be invoked from a free function;
- store the scaled QImage under both the resolved URL and the original
  resource name, so lookups on the original document (baseUrl resolves
  the name) and inside print()'s baseUrl-less clone (resolved(name) ==
  name) both hit.

HTML-to-PDF now embeds capped-resolution images regardless of the
process working directory.
Add GTest coverage for the QTextDocument+QPrinter converter, following
the existing white-box pattern (include the .cpp in a single TU behind
UT_HTMLTOPDF_ENABLED, custom moc for the anonymous-namespace handler):

- HtmlTopdfDpiTest: reference DPI from the primary screen, layout scale
  factor for non-96dpi displays
- HtmlTopdfConvertTest: basic conversion, missing input, unwritable
  output, oversized image embedded under the 300-dpi cap
- HtmlTopdfImageHandlerTest: intrinsic size, CSS-constrained size,
  clamp, aspect ratio, huge/small/missing image paths
- HtmlTopdfEmbeddingTest: loadResource scaling cap, aspect ratio, no
  scaling under the cap, missing file
- HtmlTopdfPreloadTest: preload caches images that survive the print
  clone, embedding cap propagates through the clone, empty doc no-crash

Also fix tests/main.cpp: the QCoreApplication was destroyed at the end
of its single-statement scope, so any QPrinter constructed afterwards
aborted with "Must construct a QCoreApplication before a QPrinter".
Hold it in a static unique_ptr for the whole run. And extend
test-prj-running.sh coverage extraction to htmltopdf/ alongside
reader/.

All 1287 tests pass (23 new).
Third-party libraries (djvulibre, deepin-pdfium, Qt's GL cleanup)
segfault or abort during the exit-time global destruction of the full
test binary, and gcov's atexit writeback is registered after those
destructors, so no .gcda file survives and lcov captures nothing.

RUN_ALL_TESTS already writes the gtest XML, so after it returns:
explicitly __gcov_dump() the counters, flush stdio, then _exit() to
skip the crashing exit-time destructors entirely.

Note the declaration must be a strong reference: a weak one never
makes the linker pull gcov-dump.o out of libgcov.a, leaving the pointer
null. The test target always links with -fprofile-arcs, so the symbol
always resolves.

Also accept exit 139 (SIGSEGV) alongside 134 in test-prj-running.sh's
exit-time-crash tolerance, matching the third-party teardown crashes
observed on this system.
@add-uos
add-uos force-pushed the task-v8179-refactor-htmltopdf-remove-webengine branch from ea636c9 to b84f6ea Compare November 11, 2026 06:17
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 96 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交将 htmltopdf 模块从 QtWebEngine(Chromium 引擎)重构为 QTextDocument + QPrinter 实现, 移除了重量级依赖,代码结构清晰、注释详尽,并配套了完整的白盒单元测试。 四维度均通过:语法逻辑正确无编译错误,代码质量良好(存在少量残留调试注释和死代码), 性能优于原方案(轻量文本渲染替代 Chromium),无安全漏洞。建议清理测试文件中的临时注释和死代码。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整,存在少量残留调试代码 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ["删除 ut_htmltopdfconverter.cpp 第1086行的临时注释 '// ---- 临时诊断(排查后删除) ----'", '删除 tests/main.cpp 中未使用的 #if defined(CMAKE_SAFETYTEST_ARG_ON) / #include <sanitizer/asan_interface.h> / #endif 代码块', '将 preloadImageResources 参数类型改为 EmbeddingTextDocument*,移除不必要的 static_cast', '将测试中的 QTextDocument *clone = doc.clone() 替换为 std::unique_ptr clone(doc.clone())']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 1. 删除 tests/htmltopdf/ut_htmltopdfconverter.cpp 第1086行的临时注释
// 将:
// // ---- 临时诊断(排查后删除) ----
//
// #endif  // UT_HTMLTOPDF_ENABLED
// 改为:
// #endif  // UT_HTMLTOPDF_ENABLED

// 2. 删除 tests/main.cpp 中未使用的 ASan 头文件包含
// 移除以下三行:
// #if defined(CMAKE_SAFETYTEST_ARG_ON)
// #include <sanitizer/asan_interface.h>
// #endif

// 3. 修改 preloadImageResources 参数类型
// 将:
// void preloadImageResources(QTextDocument *document)
// {
//     auto *embeddingDoc = static_cast<EmbeddingTextDocument *>(document);
// 改为:
// void preloadImageResources(EmbeddingTextDocument *document)
// {
//     auto *embeddingDoc = document;

// 4. 使用智能指针管理 clone 生命周期
// 将:
// QTextDocument *clone = doc.clone();
// ASSERT_NE(clone, nullptr);
// ...
// delete clone;
// 改为:
// std::unique_ptr<QTextDocument> clone(doc.clone());
// ASSERT_NE(clone, nullptr);
// ...
// // 无需手动 delete

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit c6d9f2b into linuxdeepin:master Nov 23, 2026
9 checks passed
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

Successfully merging this pull request may close these issues.

3 participants