php - 如何在没有打印对话框窗口的情况下自动打印数据内容(在 Div、表格、表单等上)-(WebClientPrint-Neodynamic)
问题描述
我正在关注此 URL 上的 PDF 版本:https ://www.neodynamic.com/articles/Print-PDF-from-PHP-directly-to-default-printer-without-print-dialog/
我设法让它运行起来。但是,我注意到它只能打印一个保存的 PDF 文件,使用 $filePath = 'files/LoremIpsum.pdf'; 在 PrintPDFController.php 文件下。有没有一种方法可以从数据库中动态获取内容,该数据库在表格上输出,如下所示:
<table id="WebClientPrint">
<tr><td>ONLY PRINT THIS PART AUTOMATICALLY</td></tr>
</table>
<input type="button" onclick="How to call Table ID i.e. WebClientPrint and the default printer" value="Print" />
这意味着在单击按钮时,仅应打印出表格的内容。
解决方案
您好,我曾经使用过这种类型的打印,它对我有帮助,只会弹出选择打印机的窗口。
function PrintDiv() {
var contents = document.getElementById("dvContents").innerHTML;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
return false;
}
像这样使用它
<body>
<form id="form1">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample code</span>
<hr />
<div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hello,
<br />
This is <span style="color: #18B5F0">Mateo</span>.<br />
Hoping that you are enjoying my articles!</span>
</div>
<br />
<input type="button" onclick="PrintDiv();" value="Print" />
</form>
</body>
我希望它对你有帮助。
推荐阅读
- c++ - 如何在方法体中返回已声明向量的引用?
- django - Django使用编辑表单作为添加用户给出错误
- android - 调试本机代码时,Android Studio 中的 art_sigsegv_fault 崩溃
- php - Doctrine:如何按需创建实体相关表?(如果我想保留一个 SQL 模式源)
- javascript - 从另一个ajax函数获取json编码数据
- pyspark - pyspark p 值和 chisquaretest 相关性
- php - 如何使用连接代码点火器从另一个表中获取值
- r - R 中一组检查所有适用问题的聚类或多重对应分析
- python - 如何仅覆盖 sklearn 分类器的 fit() 函数?
- gatsby - 用 gatsby 公开/重用 html 块