捕获HTML画布为gif/jpg/png/pdf?

时间:2022-03-24 09:01:27

Is it possible to capture or print what's displayed in an html canvas as an image or pdf?

是否有可能捕获或打印html画布中显示的图像或pdf?

I'd like to generate an image via canvas, and be able to generate a png from that image.

我想通过canvas生成一个图像,并能够从该图像生成一个png。

10 个解决方案

#1


628  

Oops. Original answer was specific to a similar question. This has been revised:

哦。最初的回答是针对一个类似的问题。这是修改后的:

var canvas = document.getElementById("mycanvas");
var img    = canvas.toDataURL("image/png");

with the value in IMG you can write it out as a new Image like so:

利用IMG中的值,你可以把它写成这样的新图像:

document.write('<img src="'+img+'"/>');

#2


104  

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).

HTML5提供Canvas.toDataURL(mimetype),在Opera、Firefox和Safari 4 beta中实现。但是,有许多安全限制(主要是将内容从另一个来源绘制到画布上)。

So you don't need an additional library.

所以不需要额外的库。

e.g.

如。

 <canvas id=canvas width=200 height=200></canvas>
 <script>
      window.onload = function() {
          var canvas = document.getElementById("canvas");
          var context = canvas.getContext("2d");
          context.fillStyle = "green";
          context.fillRect(50, 50, 100, 100);
          // no argument defaults to image/png; image/jpeg, etc also work on some
          // implementations -- image/png is the only one that must be supported per spec.
          window.location = canvas.toDataURL("image/png");
      }
 </script>

Theoretically this should create and then navigate to an image with a green square in the middle of it, but I haven't tested.

从理论上讲,这应该创建并导航到一个中间有一个绿色方块的图像,但我还没有测试。

#3


35  

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:

为了使画布成为图像,您应该做以下工作:

var canvas = document.getElementById("mycanvas");
var image = canvas.toDataURL("image/png");

You can use this to write the image to the page:

你可以用它把图像写到页面上:

document.write('<img src="'+image+'"/>');

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:

其中“image/png”是mime类型(png是唯一必须支持的类型)。如果您想要一个支持类型的数组,您可以按照以下步骤进行操作:

var imageMimes = ['image/png', 'image/bmp', 'image/gif', 'image/jpeg', 'image/tiff']; //Extend as necessary 
var acceptedMimes = new Array();
for(i = 0; i < imageMimes.length; i++) {
    if(canvas.toDataURL(imageMimes[i]).search(imageMimes[i])>=0) {
        acceptedMimes[acceptedMimes.length] = imageMimes[i];
    }
}

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:

如果您希望让用户下载该文件,您可以这样做:

var canvas = document.getElementById("mycanvas");
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); //Convert image to 'octet-stream' (Just a download, really)
window.location.href = image;

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.

如果您正在使用不同的mime类型,请确保更改image/png的两个实例,而不是image/octet-stream。值得一提的是,如果您在呈现画布时使用任何跨域资源,那么当您尝试使用toDataUrl方法时,您将会遇到安全错误。

#4


25  

function exportCanvasAsPNG(id, fileName) {

    var canvasElement = document.getElementById(id);

    var MIME_TYPE = "image/png";

    var imgURL = canvasElement.toDataURL(MIME_TYPE);

    var dlLink = document.createElement('a');
    dlLink.download = fileName;
    dlLink.href = imgURL;
    dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');

    document.body.appendChild(dlLink);
    dlLink.click();
    document.body.removeChild(dlLink);
}

#5


21  

I would use "wkhtmltopdf". It just work great. It uses webkit engine (used in Chrome, Safari, etc.), and it is very easy to use:

我将使用“wkhtmltopdf”。它只是伟大的工作。它使用webkit引擎(用于Chrome, Safari等),非常容易使用:

wkhtmltopdf *.com/questions/923885/ this_question.pdf

That's it!

就是这样!

Try it

试一试

#6


13  

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

使用toDataURL中锋数据

-Set the headers

设置标题

$filename = "test.jpg"; //or png
header('Content-Description: File Transfer');
if($msie = !strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false)      
  header("Content-type: application/force-download");else       
  header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$filename\"");   
header("Content-Transfer-Encoding: binary"); 
header("Expires: 0"); header("Cache-Control: must-revalidate"); 
header("Pragma: public");

-create image

——创造形象

$data = $_POST['data'];
$img = imagecreatefromstring(base64_decode(substr($data,strpos($data,',')+1)));

-export image as JPEG

出口JPEG的形象

$width = imagesx($img);
$height = imagesy($img);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $img, 0, 0, 0, 0, $width, $height);
imagejpeg($output);
exit();

-or as transparent PNG

或者透明PNG

imagesavealpha($img, true);
imagepng($img);
die($img);

#7


8  

Another interesting solution is PhantomJS. It's a headless WebKit scriptable with JavaScript or CoffeeScript.

另一个有趣的解决方案是PhantomJS。它是一个无头的WebKit脚本,带有JavaScript或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.

其中一个用例是屏幕捕获:您可以通过编程方式捕获web内容,包括SVG和Canvas,以及/或使用缩略图预览创建web站点的屏幕快照。

The best entry point is the screen capture wiki page.

最好的切入点是屏幕捕获wiki页面。

Here is a good example for polar clock (from RaphaelJS):

这里有一个极好的例子来说明极坐标时钟(来自RaphaelJS):

>phantomjs rasterize.js http://raphaeljs.com/polar-clock.html clock.png

Do you want to render a page to a PDF ?

您想要将页面呈现为PDF格式吗?

> phantomjs rasterize.js 'http://en.wikipedia.org/w/index.php?title=Jakarta&printable=yes' jakarta.pdf

#8


3  

If you are using jQuery, which quite a lot of people do, then you would implement the accepted answer like so:

如果你用的是jQuery,很多人都会这么做,那么你就会实现如下答案:

var canvas = $("#mycanvas")[0];
var img = canvas.toDataURL("image/png");

$("#elememt-to-write-to").html('<img src="'+img+'"/>');

#9


0  

You can use jspdf to capture a canvas into an image or pdf like this:

您可以使用jspdf将画布捕获到如下的图像或pdf:

var imgData = canvas.toDataURL('image/png');              
var doc = new jsPDF('p', 'mm');
doc.addImage(imgData, 'PNG', 10, 10);
doc.save('sample-file.pdf');

More info: https://github.com/MrRio/jsPDF

更多信息:https://github.com/MrRio/jsPDF。

#10


-1  

On some versions of Chrome, you can:

在一些版本的Chrome上,你可以:

  1. Use the draw image function ctx.drawImage(image1, 0, 0, w, h);
  2. 使用绘制图像函数ctx。drawImage(image1, 0,0, w, h);
  3. Right-click on the canvas
  4. 右键单击画布

#1


628  

Oops. Original answer was specific to a similar question. This has been revised:

哦。最初的回答是针对一个类似的问题。这是修改后的:

var canvas = document.getElementById("mycanvas");
var img    = canvas.toDataURL("image/png");

with the value in IMG you can write it out as a new Image like so:

利用IMG中的值,你可以把它写成这样的新图像:

document.write('<img src="'+img+'"/>');

#2


104  

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).

HTML5提供Canvas.toDataURL(mimetype),在Opera、Firefox和Safari 4 beta中实现。但是,有许多安全限制(主要是将内容从另一个来源绘制到画布上)。

So you don't need an additional library.

所以不需要额外的库。

e.g.

如。

 <canvas id=canvas width=200 height=200></canvas>
 <script>
      window.onload = function() {
          var canvas = document.getElementById("canvas");
          var context = canvas.getContext("2d");
          context.fillStyle = "green";
          context.fillRect(50, 50, 100, 100);
          // no argument defaults to image/png; image/jpeg, etc also work on some
          // implementations -- image/png is the only one that must be supported per spec.
          window.location = canvas.toDataURL("image/png");
      }
 </script>

Theoretically this should create and then navigate to an image with a green square in the middle of it, but I haven't tested.

从理论上讲,这应该创建并导航到一个中间有一个绿色方块的图像,但我还没有测试。

#3


35  

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:

为了使画布成为图像,您应该做以下工作:

var canvas = document.getElementById("mycanvas");
var image = canvas.toDataURL("image/png");

You can use this to write the image to the page:

你可以用它把图像写到页面上:

document.write('<img src="'+image+'"/>');

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:

其中“image/png”是mime类型(png是唯一必须支持的类型)。如果您想要一个支持类型的数组,您可以按照以下步骤进行操作:

var imageMimes = ['image/png', 'image/bmp', 'image/gif', 'image/jpeg', 'image/tiff']; //Extend as necessary 
var acceptedMimes = new Array();
for(i = 0; i < imageMimes.length; i++) {
    if(canvas.toDataURL(imageMimes[i]).search(imageMimes[i])>=0) {
        acceptedMimes[acceptedMimes.length] = imageMimes[i];
    }
}

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:

如果您希望让用户下载该文件,您可以这样做:

var canvas = document.getElementById("mycanvas");
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); //Convert image to 'octet-stream' (Just a download, really)
window.location.href = image;

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.

如果您正在使用不同的mime类型,请确保更改image/png的两个实例,而不是image/octet-stream。值得一提的是,如果您在呈现画布时使用任何跨域资源,那么当您尝试使用toDataUrl方法时,您将会遇到安全错误。

#4


25  

function exportCanvasAsPNG(id, fileName) {

    var canvasElement = document.getElementById(id);

    var MIME_TYPE = "image/png";

    var imgURL = canvasElement.toDataURL(MIME_TYPE);

    var dlLink = document.createElement('a');
    dlLink.download = fileName;
    dlLink.href = imgURL;
    dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');

    document.body.appendChild(dlLink);
    dlLink.click();
    document.body.removeChild(dlLink);
}

#5


21  

I would use "wkhtmltopdf". It just work great. It uses webkit engine (used in Chrome, Safari, etc.), and it is very easy to use:

我将使用“wkhtmltopdf”。它只是伟大的工作。它使用webkit引擎(用于Chrome, Safari等),非常容易使用:

wkhtmltopdf *.com/questions/923885/ this_question.pdf

That's it!

就是这样!

Try it

试一试

#6


13  

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

使用toDataURL中锋数据

-Set the headers

设置标题

$filename = "test.jpg"; //or png
header('Content-Description: File Transfer');
if($msie = !strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false)      
  header("Content-type: application/force-download");else       
  header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$filename\"");   
header("Content-Transfer-Encoding: binary"); 
header("Expires: 0"); header("Cache-Control: must-revalidate"); 
header("Pragma: public");

-create image

——创造形象

$data = $_POST['data'];
$img = imagecreatefromstring(base64_decode(substr($data,strpos($data,',')+1)));

-export image as JPEG

出口JPEG的形象

$width = imagesx($img);
$height = imagesy($img);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output,  255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $img, 0, 0, 0, 0, $width, $height);
imagejpeg($output);
exit();

-or as transparent PNG

或者透明PNG

imagesavealpha($img, true);
imagepng($img);
die($img);

#7


8  

Another interesting solution is PhantomJS. It's a headless WebKit scriptable with JavaScript or CoffeeScript.

另一个有趣的解决方案是PhantomJS。它是一个无头的WebKit脚本,带有JavaScript或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.

其中一个用例是屏幕捕获:您可以通过编程方式捕获web内容,包括SVG和Canvas,以及/或使用缩略图预览创建web站点的屏幕快照。

The best entry point is the screen capture wiki page.

最好的切入点是屏幕捕获wiki页面。

Here is a good example for polar clock (from RaphaelJS):

这里有一个极好的例子来说明极坐标时钟(来自RaphaelJS):

>phantomjs rasterize.js http://raphaeljs.com/polar-clock.html clock.png

Do you want to render a page to a PDF ?

您想要将页面呈现为PDF格式吗?

> phantomjs rasterize.js 'http://en.wikipedia.org/w/index.php?title=Jakarta&printable=yes' jakarta.pdf

#8


3  

If you are using jQuery, which quite a lot of people do, then you would implement the accepted answer like so:

如果你用的是jQuery,很多人都会这么做,那么你就会实现如下答案:

var canvas = $("#mycanvas")[0];
var img = canvas.toDataURL("image/png");

$("#elememt-to-write-to").html('<img src="'+img+'"/>');

#9


0  

You can use jspdf to capture a canvas into an image or pdf like this:

您可以使用jspdf将画布捕获到如下的图像或pdf:

var imgData = canvas.toDataURL('image/png');              
var doc = new jsPDF('p', 'mm');
doc.addImage(imgData, 'PNG', 10, 10);
doc.save('sample-file.pdf');

More info: https://github.com/MrRio/jsPDF

更多信息:https://github.com/MrRio/jsPDF。

#10


-1  

On some versions of Chrome, you can:

在一些版本的Chrome上,你可以:

  1. Use the draw image function ctx.drawImage(image1, 0, 0, w, h);
  2. 使用绘制图像函数ctx。drawImage(image1, 0,0, w, h);
  3. Right-click on the canvas
  4. 右键单击画布