I want to print a pdf file automatically when I click print button using php or javascript.
Thanks
I want to print a pdf file automatically when I click print button using php or javascript.
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.
You could a message to that effect.
Attention! Google Cloud Print is deprecated after December 2020.
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. Steps for adding your printer to Google Cloudprint
To print documents from PHP on Google Cloudprint you need following class php-google-cloud-print from Github.
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.