惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
宝玉的分享
宝玉的分享
Jina AI
Jina AI
IT之家
IT之家
博客园 - Franky
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
美团技术团队
S
SegmentFault 最新的问题
罗磊的独立博客
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
D
Docker
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence

Arch Linux Forums

Avidemux crashes without strace / Applications & Desktop Environments how to apply patches with non-linux linends / Newbie Corner Replicating CachyOS on vanilla Arch (or at least getting close) / Arch Discussion What's arch linux GUI package manager ? / Newbie Corner Hibernation failing due to insufficient memory / System Administration profiledef.sh editting question / Installation trying to script kde plasma wallpaper settings / Programming & Scripting Looking for new Audacious package maintainer / Creating & Modifying Packages issues installing arch with LUKS2 encryption / Newbie Corner QEMU PXE booting does not work with OVMF.4m.fd / Applications & Desktop Environments Wired lan regular disconnect / Newbie Corner Need Help setting up ARCH in my G16 G634JZR iwlwifi started failing consistently, trying to determine root cause Windows randomly jumping between monitors after GNOME 50 update No display via DP or HDMI after boot. / Kernel & Hardware how to change acpi platform_profile? / Newbie Corner Linux denied all kernel modules which not loaded right now Use iPhone as Webcam for Arch Linux Video Output Failure on nvidia-580xx-dkms on TTY --> Desktop switch (Page 2) / Kernel & Hardware I was going to rant ..WINE32 Sabotage compliments of Arvind Krishna / Arch Discussion [SOLVED] LUKS drive auto unlocked by TPM when expected not to / Networking, Server, and Protection Hibernate/suspend from X = dark panel; from TTY = works (ASUS G14, hyb (Page 2) / Laptop Issues Headphone jack noise/buzz / Newbie Corner segmentation fault in cc1plus when building CLK / AUR Issues, Discussion & PKGBUILD Requests Console alternative to meld / GNU/Linux Discussion Problem with paru git clone / Newbie Corner XKB questions / Applications & Desktop Environments gnome-keyring-daemon is not working correctly / Applications & Desktop Environments [SOLVED] Steam opens and immediately closes constantly / Newbie Corner Firefox rounded edges on Sway / Applications & Desktop Environments
Post your handy self made command line utilities (Page 15...
ReDress · 2026-06-14 · via Arch Linux Forums

If you already have qt6-webengine installed,

Here is a little web page to pdf cpp file, with user agent, script, image, font, paper size/margins adjustments. Good for calling from a bash script, or with args.

html2pdf.cpp

// Web page to pdf
// Needs qt6-base, qt6-webengine

#include <QApplication>
#include <QCommandLineParser>
#include <QFile>
#include <QTextStream>
#include <QWebEngineView>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QMarginsF>
#include <QPageLayout>
#include <QPageSize>

#include <functional>
#include <utility>

//User Agent iphone18 Chrome 141
const char* AgentIphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6_2 "
    "like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) "
    "CriOS/141.0.73393.39 Mobile/15E148 Safari/604.1";

//User Agent Win10 firefox 143
const char* AgentWin10 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
    "rv:143.0) Gecko/20100101 Firefox/143.0";

/*** Set User Agent, Scripts, Images, Font Size, Paper size here ***/
//Set User agent
const char* agent = AgentIphone;

//Set javascript and images on/off, font size
const bool js = false;
const bool im = true;
const int fs = 18;

//Paper sizes
//Letter 216×279, Legal 216×356, Ledger 279×432, Tabloid 432×279
//A4 210x297, A3 297x420, A2 420x594, A1 594x841

//Set Paper size, margins
const QPageLayout layout(
    //QPageSize(QPageSize::Letter), QPageLayout::Portrait,
    //QPageSize(QPageSize::Ledger), QPageLayout::Portrait,
    QPageSize(QPageSize::Tabloid), QPageLayout::Portrait,
    QMarginsF(0, 2, 0, 2), //(left, top, right, bottom)
    QPageLayout::Millimeter
);
/************************* End of Settings *************************/

class Html2PdfConverter : public QObject {
    Q_OBJECT
public:
    explicit Html2PdfConverter(QString inputPath, QString outputPath);
    int run();

private slots:
    void loadFinished(bool ok);
    void pdfPrintingFinished(const QString &filePath, bool success);

private:
    QString m_inputPath;
    QString m_outputPath;
    QScopedPointer<QWebEngineView> m_view;
};

Html2PdfConverter::Html2PdfConverter(QString inputPath, QString outputPath)
    : m_inputPath(std::move(inputPath))
    , m_outputPath(std::move(outputPath))
    , m_view(new QWebEngineView) {
    connect(m_view.data(), &QWebEngineView::loadFinished,
            this, &Html2PdfConverter::loadFinished);
    connect(m_view.data(), &QWebEngineView::pdfPrintingFinished,
            this, &Html2PdfConverter::pdfPrintingFinished);
}

int Html2PdfConverter::run() {
    m_view->load(QUrl::fromUserInput(m_inputPath));
    return QApplication::exec();
}

void Html2PdfConverter::loadFinished(bool ok) {
    if (!ok) {
        QTextStream(stderr)
            << tr("failed to load URL '%1'").arg(m_inputPath) << "\n";
        QCoreApplication::exit(1);
        return;
    }

    m_view->printToPdf(m_outputPath, layout);
}

void Html2PdfConverter::pdfPrintingFinished(const QString &filePath, bool success) {
    if (!success) {
        QTextStream(stderr)
            << tr("failed to print to output file '%1'").arg(filePath) << "\n";
        QCoreApplication::exit(1);
    } else {
        QCoreApplication::quit();
    }
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QCoreApplication::setOrganizationName("web2pdf6");
    QCoreApplication::setApplicationName("html2pdf6");
    QCoreApplication::setApplicationVersion(QT_VERSION_STR);
    
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, js);
    QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, im);
    QWebEngineProfile::defaultProfile()->settings()->setFontSize(QWebEngineSettings::DefaultFontSize, fs);
    QWebEngineProfile::defaultProfile()->settings()->setFontFamily(QWebEngineSettings::StandardFont, "monospace");
    QWebEngineProfile::defaultProfile()->setHttpUserAgent(agent);

    QCommandLineParser parser;
    parser.setApplicationDescription(
        QCoreApplication::translate("main", "Converts the web page INPUT into the PDF file OUTPUT."));
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument(
        QCoreApplication::translate("main", "INPUT"),
        QCoreApplication::translate("main", "Input URL for PDF conversion."));
    parser.addPositionalArgument(
        QCoreApplication::translate("main", "OUTPUT"),
        QCoreApplication::translate("main", "Output file name for PDF conversion."));

    parser.process(QCoreApplication::arguments());

    const QStringList requiredArguments = parser.positionalArguments();
    if (requiredArguments.size() != 2)
        parser.showHelp(1);

    Html2PdfConverter converter(requiredArguments.at(0), requiredArguments.at(1));
    return converter.run();
}

#include "html2pdf.moc"

html2pdf.pro

TEMPLATE = app

QT += webenginewidgets

SOURCES += html2pdf.cpp

target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/html2pdf
INSTALLS += target