HTML转PDF目前最优解决方案:headless chrome
作者:互联网
HTML转PDF目前最优解决方案:headless chrome
网上关于headless chrome大多是科普,只是单纯介绍其功能,一些细节仍然需要补充
前置条件:
- chrome
虽然网上都是使用的Google Chrome Canary ,但我没进行测试
const char a[] = "/c \"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe\" --headless --disable-gpu --print-to-pdf-no-header --print-to-pdf=D:/212.pdf file:///D:/1.html";
//本人的功能诉求比较简单,就是单纯需要不带页眉页脚的pdf而已
int rt = ShellExecuteA(NULL, "open", "cmd.exe",a, NULL, SW_HIDE);
参数介绍:
-
/c
这是cmd 的参数,执行完关闭窗口 -
“C:/Program Files (x86)/Google/Chrome/Application/chrome.exe”
chrome路径
设置环境变量就可以摆脱前面冗余的目录 -
–headless --disable-gpu
开启无GUI的chrome -
–print-to-pdf-no-header
不显示页眉页脚(如果需要页眉页脚直接删除此参数) -
–print-to-pdf=D:/212.pdf
转化出来的pdf存放位置 -
file:///D:/1.html
待转化的html文件路径
其中–print-to-pdf-no-header和–print-to-pdf=D:/212.pdf都是必须的,即:不可以写成–print-to-pdf-no-header=D:/212.pdf
扩展参数介绍
// The background color to be used if the page doesn't specify one. Provided as
// RGBA integer value in hex, e.g. 'ff0000ff' for red or '00000000' for
// transparent.
const char kDefaultBackgroundColor[] = "default-background-color";
// Whether cookies stored as part of user profile are encrypted.
const char kDisableCookieEncryption[] = "disable-cookie-encryption";
// Whether or not begin frames should be issued over DevToolsProtocol
// (experimental).
const char kEnableBeginFrameControl[] = "enable-begin-frame-control";
// Enable crash reporter for headless.
const char kEnableCrashReporter[] = "enable-crash-reporter";
// If enabled, generate a tagged (accessible) file when printing to PDF.
// The plan is for this to go away once tagged PDFs become the default.
// See https://crbug.com/607777
const char kExportTaggedPDF[] = "export-tagged-pdf";
// Disable crash reporter for headless. It is enabled by default in official
// builds.
const char kDisableCrashReporter[] = "disable-crash-reporter";
// The directory breakpad should store minidumps in.
const char kCrashDumpsDir[] = "crash-dumps-dir";
// A meta flag. This sets a number of flags which put the browser into
// deterministic mode where begin frames should be issued over DevToolsProtocol
// (experimental).
const char kDeterministicMode[] = "deterministic-mode";
// Use a specific disk cache location, rather than one derived from the
// UserDatadir.
const char kDiskCacheDir[] = "disk-cache-dir";
// Instructs headless_shell to print document.body.innerHTML to stdout.
const char kDumpDom[] = "dump-dom";
// Hide scrollbars from screenshots.
const char kHideScrollbars[] = "hide-scrollbars";
// Specifies which encryption storage backend to use. Possible values are
// kwallet, kwallet5, gnome, gnome-keyring, gnome-libsecret, basic. Any other
// value will lead to Chrome detecting the best backend automatically.
// TODO(crbug.com/571003): Once PasswordStore no longer uses the Keyring or
// KWallet for storing passwords, rename this flag to stop referencing
// passwords. Do not rename it sooner, though; developers and testers might
// rely on it keeping large amounts of testing passwords out of their Keyrings
// or KWallets.
const char kPasswordStore[] = "password-store";
// Save a pdf file of the loaded page.
const char kPrintToPDF[] = "print-to-pdf";
// Do not display header and footer in the pdf file.
const char kPrintToPDFNoHeader[] = "print-to-pdf-no-header";
// Specifies a list of hosts for whom we bypass proxy settings and use direct
// connections. Ignored unless --proxy-server is also specified. This is a
// comma-separated list of bypass rules. See:
// "net/proxy_resolution/proxy_bypass_rules.h" for the format of these rules.
const char kProxyBypassList[] = "proxy-bypass-list";
// Uses a specified proxy server, overrides system settings. This switch only
// affects HTTP and HTTPS requests.
const char kProxyServer[] = "proxy-server";
// Do not use system proxy configuration service.
const char kNoSystemProxyConfigService[] = "no-system-proxy-config-service";
// Use the given address instead of the default loopback for accepting remote
// debugging connections. Should be used together with --remote-debugging-port.
// Note that the remote debugging protocol does not perform any authentication,
// so exposing it too widely can be a security risk.
const char kRemoteDebuggingAddress[] = "remote-debugging-address";
// Runs a read-eval-print loop that allows the user to evaluate Javascript
// expressions.
const char kRepl[] = "repl";
// Save a screenshot of the loaded page.
const char kScreenshot[] = "screenshot";
// Causes SSL key material to be logged to the specified file for debugging
// purposes. See
// https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
// for the format.
const char kSSLKeyLogFile[] = "ssl-key-log-file";
// Issues a stop after the specified number of milliseconds. This cancels all
// navigation and causes the DOMContentLoaded event to fire.
const char kTimeout[] = "timeout";
// Sets the GL implementation to use. Use a blank string to disable GL
// rendering.
const char kUseGL[] = "use-gl";
// A string used to override the default user agent with a custom one.
const char kUserAgent[] = "user-agent";
// Directory where the browser stores the user profile.
const char kUserDataDir[] = "user-data-dir";
// If set the system waits the specified number of virtual milliseconds before
// deeming the page to be ready. For determinism virtual time does not advance
// while there are pending network fetches (i.e no timers will fire). Once all
// network fetches have completed, timers fire and if the system runs out of
// virtual time is fastforwarded so the next timer fires immediatley, until the
// specified virtual time budget is exhausted.
const char kVirtualTimeBudget[] = "virtual-time-budget";
// Sets the initial window size. Provided as string in the format "800,600".
const char kWindowSize[] = "window-size";
// Allowlist for Negotiate Auth servers.
const char kAuthServerAllowlist[] = "auth-server-whitelist";
// Sets font render hinting when running headless, affects Skia rendering and
// whether glyph subpixel positioning is enabled.
// Possible values: none|slight|medium|full|max. Default: full.
const char kFontRenderHinting[] = "font-render-hinting";
// If true, then all pop-ups and calls to window.open will fail.
const char kBlockNewWebContents[] = "block-new-web-contents";
此为headless chrome的命令(绝对原汁原味),包括非常多的功能,需要者自取
headless chrome(需要梯子,没梯子看上面代码即可)
以上就是全部内容,希望大家不要和我走一样的弯路,节约时间
能用Google就不要bing,能用bing就别用百度,搜索引擎是发散思维,百度用着用着就成信息茧房了。
标签:const,chrome,char,headless,HTML,print,pdf 来源: https://blog.csdn.net/xinzhi111/article/details/110383900