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.
Related
I have local php website that has some online and some offline components. It pulls xml data from various websites normally. When offline, the page will not render due to various errors.
I use $xml = simplexml_load_file($url); to retrieve xml data. When offline I usually get errors telling me that the target url doesn't exist and that certain nodes in the xml file cannot be retrieved.
How do I bypass these errors so that the rest of the page can be loaded when offline?
I solved the issue.
$var = #simplexml_load_file($url);
if(!$var){
echo "error";
}
else{
execute code...
}
Now when I'm offline, the script will leave an error message if it cannot connect and continue processing the page. Before, it would reach an error and stop loading the rest of the page.
I once was a Web-Designer who knew HTML/CSS. Now I'm a 3D animator, but I want to get back into the Web-Developer world.
But there's so much new to learn. E.g. flat file cms. Wow!
But my question for now is how I read an API, create the right PHP file to pull an XML file and put that data onto a web page.
Specificially I'm interested in this mobile.de API:
http://services.mobile.de/manual/search-api.html
And it seems that this is the XML that I need:
http://services.mobile.de/schema/ad-1.0.xsd
What are the next steps to get this beginner's project going?
I guess I need some sort of PHP file that uses GET and some sort of authentication. How can I test, if and what will come back?
And how do I use the pulled information to put in into a new page?
Or is my thinking all wrong?
Many thanks in advance.
Ben
Little bit you can understand through this post and answers on this post:
How to echo xml file in php
If you don't mind using already created library, please check : PHP Curl Class
Taken from the readme:
PHP Curl Class is an object-oriented wrapper of the PHP cURL extension that makes it easy to send HTTP requests and integrate with web APIs.
And this code snippet (also taken from the readme) could be your starting point:
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('');
$curl->setReferrer('');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('http://www.example.com/');
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
}
else {
echo $curl->response;
}
var_dump($curl->requestHeaders);
var_dump($curl->responseHeaders);
Oh, and it's an unlicensed license type software.
I'm trying to read a Doc from Google with an authorized user and output the content through my page.
Everything works fine, and the output is Ok, but after few seconds, a popup window appears with an error "Google Docs has encountered an error. We are looking into the problem now. Please try one of these interim solutions: Reload this page"
My code is this:
<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Docs');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_Docs_Query');
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client_grabmark = Zend_Gdata_ClientLogin::getHttpClient('MYUSER#gmail.com', 'MYPASS', $service);
$service = new Zend_Gdata_Docs($client_grabmark);
$service->setMajorProtocolVersion(3);
$contentLink = 'https://docs.google.com/document/d/'DOCID'/edit';
$data = $service->get($contentLink)->getBody();
ob_start();
echo $data;
ob_flush();
exit
?>
What I'm doing wrong? If I try export to PDF everything is Ok (I just have to change the link), but I want to show the page as if I was in Google Docs.
Any help?
Best Regards
DF
You cannot output the HTML content of a Google Doc page into the context of your own page and get that to work. If you want to show a document to a user, redirect them to the Google Doc itself, using an HTTP Location header.
<?php
header("Location: $contentLink");
?>
If the user you had intended to display the doc to does not have permission to access the doc, there are two options:
Share the doc to the user using the Documents List API.
Export the document as text, HTML, or PDF using the API, and then display it to the user.
These options are detailed in the documentation.
I just stumbled over a problem using the Dropbox API.
Currently I am using the this lib: http://code.google.com/p/dropbox-php/
The problem is, if I upload a file I get a success but nothing arrives at the Dropbox.
I also "hacked" into the library and changed some of the post stuff. I can post it if you want.
Anybody got a solution for that?
Thanks!
//EDIT: I am using PHPs integrated OAuth functions
//EDIT2:
<?php
include "class/Dropbox/autoload.php";
$oauth = new Dropbox_OAuth_PHP("xxxxxxxxxxx", "xxxxxxxxxxx");
$dropbox = new Dropbox_API($oauth);
//re-login
if(isset($_SESSION["oauth_tokens"])) {
$my->oauth->setToken($_SESSION["oauth_tokens"]);
}
//uploads itself to DB
if($dropbox->putFile( basename(__FILE__), __FILE__ )) {
echo "Success";
} else {
echo "Fail :(";
}
sure this is not the complete code but assume that the user is logged in. :)
Dropbox for PHP, the library you're using to upload files to Dropbox, isn't maintained anymore and therefore may not function properly.
Try out PHP Dropbox Uploader. It is mentioned on the official Dropbox wiki.
I am using Google Adwords to push and track subscriber acquisitions on my site, but can not get the conversion reporting to work.
Here is my setup.
My subscribe form is dynamically loaded on my subscribe page by a Wordpress plugin I created. After validation the form is replaced with a thank you message with php, so the user is not redirected to a confirmation page. Because of this, I think I need to check for the output of a conversion value to get Adwords to report.
I have followed the adwords help docs and have made the reporting a "purchase/sale" tracking so I can test for the conversion_value variable.
Here is my plugin code that outputs the thank you message.
//****[ Variable Conversion Value For Google AdWords]****
$variableConversionValue = '<h3 class="subscribeConfirm">Thanks for signing up!</h3>';
//****[ After writing subscriber data, display thank you message****
echo $variableConversionValue;
Here is my Adwords Tracking (did not include generic tracking code) that is on my subscribe page (is a php page, but the tracking in not contained within php tags).
if (<? echo $variableConversionValue; ?>) {
google_conversion_value = <? echo $variableConversionValue; ?>;
}
Can you please help me get the reporting to work? Thanks!
I'm not sure you are understanding the purpose of the conversion values. If you are looking for a binary "the conversion occurred", I'm not sure you need to specify a value at all, but if you do, you should be using a number like 1 or whatever you feel the dollar value of a lead is. I may be wrong, but I don't think Adwords will be able to process text/html in the value as anything meaningful.
If your code for checking the form and output are in two different places (as it looks like from above), maybe what you really want in your output is:
if (<? echo $variableConversionValue; ?>) {
google_conversion_value = <? echo '1'; ?>;
}
I have figured it out.
When viewing my live page source with the code above, the echo $variableConversionVale code was blank, meaning it was not carrying the value over correctly. So instead of using the variable, I put in exactly what would be displayed to track the conversion.
Example:
if ('<h3 class="subscribeConfirm">Thanks for signing up!</h3>') {
google_conversion_value = '<h3 class="subscribeConfirm">Thanks for signing up!</h3>'; />
}
Using the above code, it tracks a conversion only when the confirmation that you have been subscribed pops up.
Reference link here under step 4 "Different scenarios for inserting the code snippet"
Thanks!