1

I want to print a pdf file automatically when I click print button using php or javascript.

Thanks

2

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

I have same problem. Did you found solution?
Same boat, I'd like remote employees to be able to print in the office using our online dashboard. Google Cloud Print worked well enough, what should I do now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.