正确的html2canvas代理脚本 [英] Correct proxy script for html2canvas
问题描述
我正在尝试获取 html2render ( http://html2canvas.hertzen.com/)以在我的网站上工作.它适用于基本内容-但没有图像显示(因为它们位于cdn.domain.com子域上).
I'm trying to get html2render ( http://html2canvas.hertzen.com/ ) to work on my website. It works for the basic stuff - but none of the images show (as they are on a cdn.domain.com subdomain).
我一直在阅读,似乎不喜欢其他域名.我找到了两个PHP代理脚本:
I've been reading up, and it seems that it doesn't like other domains out of the box. I found a couple of PHP proxy scripts:
- https://github.com/brcontainer/html2canvas-php-proxy
- https://github.com/adjdred/html2canvas-proxy-php
- https://github.com/brcontainer/html2canvas-php-proxy
- https://github.com/adjdred/html2canvas-proxy-php
我试图解决这些问题,但是我们没有在此服务器上启用 curl .因此,我求助于用Perl编写一些东西.
I tried to get those going, but we don't have curl enabled on this server. So, I'm resorting to writing something in Perl.
这是到目前为止我得到的:
Here is what I've got so far:
use MIME::Base64;
use File::Slurp;
handle();
sub handle {
print ('Access-Control-Max-Age:' . 5 * 60 * 1000);
print ("Access-Control-Allow-Origin: *");
print ('Access-Control-Request-Method: *');
print ('Access-Control-Allow-Methods: OPTIONS, GET');
print ('Access-Control-Allow-Headers *');
print ("Content-Type: application/javascript");
#print $IN->header;
my $url = $IN->param('url');
$url =~ s|https://cdn.xxx.net|/srv/www/xxx.net/www|g;
if (-e $url) {
my $file = read_file($url);
use JSON;
my $mime_type;
if ($url =~ /\.jpe?g$/i) {
$mime_type = "image/jpg"
} elsif ($url =~ /\.png$/i) {
$mime_type = "image/png"
}
print JSON::encode_json([{
"pathinfo" => $url,
"error" => undef,
"data" => encode_base64($file),
"mime_type" => $mime_type
}]);
} else {
print "ACK!";
}
}
但是,它仍然不起作用:(代理上的文档很少(没有?)(除了在某些情况下告诉您您需要它!)
However, it still doesn't work :( There is little (none??) documentation on the proxy (apart from telling you that you need it in some cases!)
任何人都可以共享输出数据的外观吗?我尝试根据上面的示例代码进行计算,但是我的PHP有点生锈(并且我没有启用PHP和Curl的服务器,因此可以对其进行测试)
Can anyone share what the outputted data should look like? I tried to work it out based on the above example codes , but my PHP is a bit rusty (and I don't have a server with PHP and Curl enabled, that I can test it out on)
谢谢!
推荐答案
好吧,关于代理的答案不是很多-但我确实遇到了一个帖子:
Ok, well not so much an answer about the proxy - but I did come across a post:
在S3和CloudFront中具有CORS的HTML2Canvas
在这里,显示了一个使用示例:
In here, is shows an example of using:
useCORS: true,
...所以我尝试了:
... so I tried that:
html2canvas(document.body, {
useCORS: true,
onrendered: function(canvas) {
}
});
...而且有效!!!!
...and it works!!!!
在我的实例中,我只是使用domain.com和cdn.domain.com作为CDN.如果您使用的是其他第三方CDN,则可能需要考虑启用COR头.
In my instance, I was just using domain.com and cdn.domain.com as the CDN. If you are using other 3rd party CDN's, you may need to look at enabling the CORs headers.
这篇关于正确的html2canvas代理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!