将 HTML 画布捕获为 GIF/JPG/PNG/PDF?
是否可以将 HTML 画布中显示的内容捕获或打印为图像或 PDF?
我想通过画布生成图像,并能够从该图像生成 PNG。
Is it possible to capture or print what's displayed in an HTML canvas as an image or PDF?
I'd like to generate an image via canvas and be able to generate a PNG from that image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
原来的答案是针对类似问题的。 这已被修改:
使用
img
中的值,您可以将其写为新图像,如下所示:或
Original answer was specific to a similar question. This has been revised:
With the value in
img
you can write it out as a new image like so:or
HTML5 提供了 Canvas.toDataURL(mimetype),它在 Opera、Firefox 和 Safari 4 beta 中实现。 然而,存在许多安全限制(主要与将内容从另一个来源绘制到画布上有关)。
所以你不需要额外的库。
例如,
理论上这应该创建然后导航到中间有绿色方块的图像,但我还没有测试过。
HTML5 provides Canvas.toDataURL(mimetype) which is implemented in Opera, Firefox, and Safari 4 beta. There are a number of security restrictions, however (mostly to do with drawing content from another origin onto the canvas).
So you don't need an additional library.
e.g.
Theoretically this should create and then navigate to an image with a green square in the middle of it, but I haven't tested.
我想我应该稍微扩展一下这个问题的范围,并提供一些关于此事的有用花絮。
为了将画布作为图像,您应该执行以下操作:
您可以使用它将图像写入页面:
其中“image/png”是一种 mime 类型(png 是唯一必须支持的类型)。 如果您想要一个受支持类型的数组,您可以按照以下方式执行操作:
您只需每个页面运行一次 - 它在页面的生命周期中永远不会改变。
如果您希望用户在保存文件时下载文件,您可以执行以下操作:
如果您将其与不同的 mime 类型一起使用,请务必更改 image/png 的两个实例,但不要更改 image/octet-stream 。
还值得一提的是,如果您在渲染画布时使用任何跨域资源,则在尝试使用 toDataUrl 方法时将遇到安全错误。
I thought I'd extend the scope of this question a bit, with some useful tidbits on the matter.
In order to get the canvas as an image, you should do the following:
You can use this to write the image to the page:
Where "image/png" is a mime type (png is the only one that must be supported). If you would like an array of the supported types you can do something along the lines of this:
You only need to run this once per page - it should never change through a page's lifecycle.
If you wish to make the user download the file as it is saved you can do the following:
If you're using that with different mime types, be sure to change both instances of image/png, but not the image/octet-stream.
It is also worth mentioning that if you use any cross-domain resources in rendering your canvas, you will encounter a security error when you try to use the toDataUrl method.
我会使用“wkhtmltopdf”。 它工作得很好。 它使用webkit引擎(在Chrome、Safari等中使用),并且非常容易使用:
就是这样!
尝试一下
I would use "wkhtmltopdf". It just work great. It uses webkit engine (used in Chrome, Safari, etc.), and it is very easy to use:
That's it!
Try it
如果您通过服务器下载,这里有一些帮助(这样您可以命名/转换/后处理/等您的文件): -
使用
toDataURL
发布数据- 设置标头
- 创建图像
-将图像导出为 JPEG
- 或作为透明PNG
Here is some help if you do the download through a server (this way you can name/convert/post-process/etc your file):
-Post data using
toDataURL
-Set the headers
-create image
-export image as JPEG
-or as transparent PNG
这是另一种方式,没有字符串,尽管我真的不知道它是否更快。 而不是 toDataURL (正如这里所有问题所建议的那样)。 就我而言,想要阻止 dataUrl/base64,因为我需要数组缓冲区或视图。 因此 HTMLCanvasElement 中的另一个方法是 toBlob。 (TypeScript 函数):
blob 的另一个优点是您可以创建 ObjectUrls 将数据表示为文件,类似于 HTMLInputFile 的“files”成员。 更多信息:
https://developer.mozilla.org/en/docs/ Web/API/HTMLCanvasElement/toBlob
This is the other way, without strings although I don't really know if it's faster or not. Instead of toDataURL (as all questions here propose). In my case want to prevent dataUrl/base64 since I need a Array buffer or view. So the other method in HTMLCanvasElement is
toBlob
. (TypeScript function):Another advantage of blobs is you can create ObjectUrls to represent data as files, similar to HTMLInputFile's 'files' member. More info:
https://developer.mozilla.org/en/docs/Web/API/HTMLCanvasElement/toBlob
另一个有趣的解决方案是 PhantomJS。
它是一个无头 WebKit,可使用 JavaScript 或 CoffeeScript 编写脚本。
用例之一是屏幕捕获:您可以以编程方式捕获 Web 内容,包括 SVG 和 Canvas 和/或创建具有缩略图预览的网站屏幕截图。
最好的入口点是屏幕截图 wiki 页面。
这是极地时钟的一个很好的例子(来自 RaphaelJS):
你想将页面渲染为 PDF 吗?
Another interesting solution is PhantomJS.
It's a headless WebKit scriptable with JavaScript or CoffeeScript.
One of the use case is screen capture : you can programmatically capture web contents, including SVG and Canvas and/or Create web site screenshots with thumbnail preview.
The best entry point is the screen capture wiki page.
Here is a good example for polar clock (from RaphaelJS):
Do you want to render a page to a PDF ?
如果您使用 jQuery(很多人都这样做),那么您将像这样实现接受的答案:
If you are using jQuery, which quite a lot of people do, then you would implement the accepted answer like so:
关键点是
我我想为像我这样想要将 SVG 保存为 PNG 的人提供一个示例(如果您愿意,也可以添加一些文本),该示例可能来自在线源或很棒的字体图标等。
示例
100% javascript,没有其他 3 -rd 图书馆。
The key point is
And I want to provide an example for someone like me who wants to save SVG to PNG(also can add some text if you wish), which may be from an Online source or font-awesome icon, etc.
Example
100% javascript and no other 3-rd library.
if you want to run on stackoverflow and move your mouse on the picture may get error
You can copy the code on your local machine and run it again, will be fine.
从
上传图像
:upload image
from<canvas />
:在某些版本的 Chrome 上,您可以:
ctx.drawImage(image1, 0, 0, w, h);
On some versions of Chrome, you can:
ctx.drawImage(image1, 0, 0, w, h);
您可以使用 jspdf 将画布捕获为图像或 pdf,如下所示:
更多信息:https://github.com /MrRio/jsPDF
You can use jspdf to capture a canvas into an image or pdf like this:
More info: https://github.com/MrRio/jsPDF
简单的答案就是获取它的 blob 并将 img src 设置为该 blob 的新对象 URL,然后使用某个库将该图像添加到 PDF,例如
或者,如果您想使用低级字节数据,您可以获取画布的原始字节,然后根据文件规范,将原始图像数据写入数据的必要字节中。 你只需要调用 ctx.getImageData(0, 0, ctx.canvas.widht, ctx.canvas.height) 来获取原始图像数据,然后根据文件规范将其写入
The simple answer is just to take the blob of it and set the img src to a new object URL of that blob, then add that image to a PDF using some library, like
Alternatively, if you wanted to work with low-level byte data, you can get the raw bytes of the canvas, then, depending on the file spec, write the raw image data into the necessary bytes of the data. you just need to call
ctx.getImageData(0, 0, ctx.canvas.widht, ctx.canvas.height)
to get the raw image data, then based on the file specification, write it to that如果你想嵌入画布,你可以使用这个片段
if you want to emebed the canvas you can use this snippet
捕获到 Gif (unpacked) 是一个 Chrome 扩展,用于将 HTML 画布记录到 GIF 文件
后处理:您需要剪切和压缩视频(ffmpeg、kdenlive、...)
为什么要压缩? 结果将具有恒定的帧速率,而不是原始帧速率。 这是所有画布记录工具的限制(ccapture、jsgif, gif 捕获画布, ...)
Capture to a Gif (unpacked) is a chrome extension to record an HTML canvas to a GIF file
postprocessing: you will need to cut and compress the video (ffmpeg, kdenlive, ...)
why compress? the result will have a constant framerate, not the original framerate. this is a limitation of all canvas-recording-tools (ccapture, jsgif, gif-capture-canvas, ...)