简体   繁体   English

如何在 php 或 javascript 中打印 pdf 文件?

[英]how to print a pdf file in php or javascript?

I want to print a pdf file automatically when I click print button using php or javascript.当我使用 php 或 javascript 单击打印按钮时,我想自动打印 pdf 文件。

Thanks谢谢

You don't need to add a button.您不需要添加按钮。 If you are loading a PDF for display on a web page by embedding it or providing a link to open in a new window, unless the document has printing disabled, the visitor can simply right-click and use the Adobe Reader print commands.如果您通过嵌入 PDF 或提供在新窗口中打开的链接加载要在网页上显示的 PDF,除非该文档已禁用打印,否则访问者只需右键单击并使用 Adob​​e Reader 打印命令即可。

You could a message to that effect.你可以发一条这样的消息。

Attention!注意力! Google Cloud Print is deprecated after December 2020. Google 云打印在 2020 年 12 月之后被弃用。

I know it's not the best solution for your problem, but if your printer has native google cloudprint support or you add the printer to cloudprint via your google chrome browser you can print documents via google.我知道这不是解决您问题的最佳解决方案,但是如果您的打印机具有本地 google cloudprint 支持,或者您通过 google chrome 浏览器将打印机添加到 cloudprint,则可以通过 google 打印文档。 Steps for adding your printer to Google Cloudprint 将打印机添加到 Google Cloudprint 的步骤

To print documents from PHP on Google Cloudprint you need following class php-google-cloud-print from Github.要在 Google Cloudprint 上从 PHP 打印文档,您需要遵循 Github 中的php-google-cloud-print类。

Information about how to get the credentials is in the Readme file of the class.有关如何获取凭据的信息在类的自述文件中。

With this code snippet the printing works like a charm.使用此代码片段,打印效果非常好。

require_once 'php/cprint/Config.php';
require_once 'php/cprint/GoogleCloudPrint.php';

$gcp = new GoogleCloudPrint();
$refreshTokenConfig['refresh_token'] = 'your_refresh_token';
$token = $gcp->getAccessTokenByRefreshToken($urlconfig['refreshtoken_url'],http_build_query($refreshTokenConfig));
$gcp->setAuthToken($token);
$printers = $gcp->getPrinters();
//print_r($printers); // Show available printers with IDs

if(count($printers) == 0){
    exit("Could not get printers");
}else{
    $printerID = "your_printer_id";
    $resarray = $gcp->sendPrintToPrinter($printerID, "Document title", "path/to/document.pdf", "application/pdf");
    if($resarray['status'] == true){
        echo "Document has been sent to printer and should print shortly.";
    }else{
        echo "An error occured while printing the doc. Error code:".$resarray['errorcode']." Message:".$resarray['errormessage'];
    }
}

I hope that this helps someone, because i searched desperately for an method to print from my webserver.我希望这对某人有所帮助,因为我拼命寻找一种从我的网络服务器打印的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM