我正在尝试使用 jsPDF 将应用程序导出为 PDF。浏览网页后,在这里抓取一行代码,在那里抓取一个部分 - 我已经设法让它工作了......有点。
它适用于 Firefox 和 Safari,但不适用于 Chrome。
使用的 JS 文件(来自 jsPDF)。也许矫枉过正。与 Jquery 一起。
<script type="text/javascript" src="PDF/standard_fonts_metrics.js"></script>
<script type="text/javascript" src="PDF/split_text_to_size.js"></script>
<script type="text/javascript" src="PDF/from_html.js"></script>
<script type="text/javascript" src="PDF/addhtml.js"></script>
<script type="text/javascript" src="PDF/addimage.js"></script>
我使用的代码是这样的:
<script type="text/javascript">
function demoFromHTML() {
$('#listAreaPDF').css("display", "block");
var pdf = new jsPDF('p', 'pt', 'letter');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('#listAreaPDF')[0];
pdf.setFontSize(24);
pdf.text(35, 40, "PDF Title here");
pdf.setFontSize(10);
pdf.text(500, 40, "Company Name AB");
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, {// y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
html2canvas($("#presentationArea"), {
onrendered: function (canvas) {
var imgData = canvas.toDataURL(
'image/png');
pdf.addImage(imgData, 'PNG', 20, 300);
pdf.save('Test.pdf');
}
});
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
// pdf.save('Test.pdf');
},
margins
);
$('#listAreaPDF').css("display", "none");
}
</script>
Credit for the code found here.为了适应我的应用程序,我做了一些小改动,添加了一个到 html2canvas 的连接,以将图像从我的应用程序中提取出来并将其放入 PDF 中。这实际上工作正常 - 在 Safari 和 Firefox 中。
在 Chrome 中单击并激活此功能时,我什至没有收到空白 PDF,我什么也得不到。甚至不会生成弹出窗口或页面。
可能是什么原因导致 Firefox 和 Safari 可以运行,但 Chrome 不能运行? 我还没有尝试过 Internet Explorer,但我并没有屏住呼吸。 如果您知道实现它的方法,我完全赞成。
欢迎您提供任何帮助或建议。
最佳答案
这个问题可能与 chrome 的 deprecation of top-frame navigation 有关.
删除:内容启动顶层框架导航到数据 URL(已删除)
We intend to block web pages from loading data: URLs in the top frame using tags, window.open, window.location and similar mechanisms.
Pseudo URLs such as data: are generally a source of confusion for users. Because of their unfamiliarity, these schemes are widely being used in spoofing and phishing attacks. Users browsing the web ideally should only ever end up on the two well known schemes (http and https).
Deprecated in M58
Removal in M60
https://www.chromestatus.com/feature/5669602927312896
如上述 Google 群组帖子中所述,一个可能的解决方案是使用 iFrame。
关于javascript - jsPDF 不适用于 Chrome,仅适用于 Firefox 和 Safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32763109/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
当我使用has_one时,它工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290
我正在使用Watir运行一个Ruby脚本来为我自动化一些事情。我试图自动将一些文件保存到某个目录。因此,在我的Mozilla设置中,我将默认下载目录设置为桌面并选择自动保存文件。但是,当我开始运行我的脚本时,这些更改并没有反射(reflect)出来。似乎首选项恢复为默认值。我已经包括以下内容require"rubygems"#Optional.require"watir-webdriver"#Forwebautomation.require"win32ole"#Forfilesavedialog.并打开一个新的firefox实例:browser=Watir::Browser.new(:
我正在使用带有Rails的Devise,我想添加一个方法“getAllComments”,所以我这样写:classUser在我的Controller中:defdashboard@user=current_user@comments=@user.getAllComments();end当我访问我的url时,我得到了undefinedmethod`getAllComments'for#我做错了什么?谢谢 最佳答案 因为getAllComments是一个类方法,而您正试图将其作为实例方法访问。您要么需要访问它:User.getAllCom
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我正在使用Rails3.2.3和Ruby1.9.3p0。我发现我经常需要确定某个字符串是否出现在选项列表中。看来我可以使用Ruby数组.includemethod:或正则表达式equals-tildematchshorthand用竖线分隔选项:就性能而言,一个比另一个好吗?还有更好的方法吗? 最佳答案 总结:Array#include?包含String元素,在接受和拒绝输入时均胜出,对于您的示例只有三个可接受的值。对于要检查的更大的集合,看起来Set#include?和String元素可能会获胜。如何测试我们应该根据经验对此进行测试
我将Cucumber与Ruby结合使用。通过Selenium-Webdriver在Chrome中运行测试时,我想将下载位置更改为测试文件夹而不是用户下载文件夹。我当前的chrome驱动程序是这样设置的:Capybara.default_driver=:seleniumCapybara.register_driver:seleniumdo|app|Capybara::Selenium::Driver.new(app,:browser=>:chrome,desired_capabilities:{'chromeOptions'=>{'args'=>%w{window-size=1920,1
Ruby初学者努力简单地将这个@@people散列的值打印到控制台classPerson#haveafirst_nameandlast_nameattributewithpublicaccessorsattr_accessor:first_nameattr_accessor:last_name#haveaclassattributecalled`people`thatholdsanarrayofobjects@@people=[]#havean`initialize`methodtoinitializeeachinstancedefinitialize(first_name,last_