gpt4 book ai didi

javascript - 如何打印当前文件以外的另一个文件?

转载 作者:行者123 更新时间:2023-11-28 03:00:48 26 4
gpt4 key购买 nike

我认为这个简单的设置会起作用并使用备用链接来打印 test.pdf。但是在打印预览中我看到的是当前页面而不是 test.pdf???

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="alternate" media="print" href="http://localhost/mydomain.com/test/test.pdf">
<script>
window.print();
</script>
</head>
<body>
<div>TODO write content</div>
</body>
</html>

[编辑]我得到的最接近的似乎只能在 Chrome 中工作(我喜欢跳过打印预览和自动打印,但 --kiosk 似乎无法与以下代码一起使用):

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script src="js/jquery-2.2.0.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function ($) {
function print(url)
{
var _this = this,
iframeId = 'iframeprint',
$iframe = $('iframe#iframeprint');
$iframe.attr('src', url);

$iframe.load(function () {
callPrint(iframeId);
});
}

//initiates print once content has been loaded into iframe
function callPrint(iframeId) {
var PDF = document.getElementById(iframeId);
PDF.focus();
PDF.contentWindow.print();
}
print('http://localhost/mydomain.com/tests/test.pdf');
});
</script>
</head>
<body onload="">
<div>TODO write content</div>
<iframe id="iframeprint" src=""></iframe>
</body>
</html>

最佳答案

您不能对 pdf 文件执行此操作。但是您可以对 html 文件执行此操作。

像这样:

function print() {
var w = window.open('', 'width=400, height=400');
w.document.body.innerHTML = "HTML";
w.print();
}
<button onclick="print()">Print another file</button>

你可以在这里看到:http://output.jsbin.com/kefoxo

完整代码:(在 Google Chrome 上测试)

http://jsbin.com/kefoxo/edit?html,js

关于javascript - 如何打印当前文件以外的另一个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com