如何在Chrome扩展程序中将mHTML转换为pdf? [英] How to convert mHTML to pdf in chrome extension?

查看:141
本文介绍了如何在Chrome扩展程序中将mHTML转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在本地运行的chrome扩展程序.我希望以编程方式在chrome标签中将活动页面打印为pdf",并保存到本地驱动器(或上传到Google云端硬盘).

I have a chrome extension running locally. I would like it to "print-to-pdf" active page in a chrome tab programmatically and save to local drive (or upload to Google Drive).

目前,我只能做到这一点:

Currently I was able to achieve only this:

  • 它使用 saveAsMHTML 使用以下代码为活动页面制作快照 chrome.pageCapture.saveAsMHTML API
  • 然后使用下载 chrome.downloads.download API到我的本地驱动器.
  • It makes a snapshot of an active page using saveAsMHTML using chrome.pageCapture.saveAsMHTML API
  • then downloads it using chrome.downloads.download API to my local drive.

如何将mHTML转换为pdf?有没有JavaScript库?还是我可以在Mac上运行的命令行?

How can I convert mHTML into pdf? Is there a javascript library? Or a command line too that I could run on my Mac?

任何解决方案都行得通,不打算仅出于我自己的需要而发布此扩展,因此不关心安全性等.

Any solution would work, not planning to publish this extension, just for my own needs, so not concerned about security, etc.

推荐答案

扩展中可以使用两种打印到PDF"方法.

There are two "print-to-PDF" methods you can use in an extension.

内容脚本中使用 window.print()显示一个对话框,您可以在其中打印为PDF.

Use window.print() in the content script to display a dialog where you can print to PDF.

使用 chrome.debugger API 并发出远程调试协议命令

Use chrome.debugger API and emit a remote debugging protocol command Page.printToPDF.

这篇关于如何在Chrome扩展程序中将mHTML转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆