Use googledocs and phpcurl to convert html to pdf

Source: Internet
Author: User
Tags hex code
Why? Phperror_reporting (E_ERROR | E_WARNING | E_PARSE); set_time_lim

Replace the google user name, password with your gmail user name and password because the csdn speed is very, very slow, back up this article to http://hi.baidu.com/beimuaihui/blog/item/9220a63a58e3dbe414cecb45.html? Phperror_reporting (E_ERROR | E_WARNING | E_PARSE); set_time_lim

Replace the google user name and password with your gmail user name and password

Since csdn is very, very slow, back up this article to http://hi.baidu.com/beimuaihui/blog/item/9220a63a58e3dbe414cecb45.html

<? Phperror_reporting (E_ERROR | E_WARNING | E_PARSE); set_time_limit (0); if (! $ _ POST ['content']) {showForm (); exit;} function getGoogleType ($ type) {$ SUPPORTED_FILETYPES = array ('txt '=> 'text/plain ', 'csv' => 'text/csv', 'tsv' => 'text/tab-separated-values ', 'tab '=> 'text/TAB-separated-values', 'html' => 'text/html', 'htm' => 'text/html ', 'Doc' => 'application/msword', 'ods '=> 'application/vnd. oasis. opendocument. spreadsheet ', 'odt' => 'application/vnd. oasis. opendocument. text', 'rtf '=> 'application/RTF', 'sxw' => 'application/vnd. sun. xml. writer ', 'xls' => 'application/vnd. ms-excel ', 'xlsx' => 'application/vnd. ms-excel ', 'ppt' => 'application/vnd. ms-powerpoint ', 'pps' => 'application/vnd. ms-powerpoint '); return $ SUPPORTED_FILETYPES [strtoupper ($ type)];}/** get auth token **/function getAuth ($ curl, $ clientlogin_post) {// Initialize the curl object // Set some options (some for SHTTP) curl_setopt ($ curl, CURLOPT_POST, true); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ clientlogin_post ); curl_setopt ($ curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // Execute $ response = curl_exec ($ curl); // Get the Auth string and save itpreg_match ("/Auth = ([a-zA-Z0-9 _/-] +)/I ", $ response, $ matches); $ auth = $ matches [1]; return $ auth;}/** get feed list **/function getEntry ($ curl, $ url, $ headers) {// Make the requestcurl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, expires, $ headers); curl_setopt ($ curl, CURLOPT_POST, false ); $ response = curl_exec ($ curl); // curl_close ($ curl); // Parse the response $ response = simplexml_load_string ($ response ); // echo '<br/> response content: <br/> <pre>'; // print_r ($ response); // echo '<br/> '; // Output dataforeach ($ response-> entry as $ file) {echo "File :". $ file-> title. "<br/>"; echo "Type :". $ file-> content ["type"]. "<br/>"; echo "Author :". $ file-> author-> name. "<br/>"; break;} return $ file;} function exportEntry ($ curl, $ url, $ headers) {// parse_str (parse_url ($ alternateLink, PHP_URL_QUERY), $ query); // Make the request curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, expires, $ headers); curl_setopt ($ curl, CURLOPT_POST, false); $ response = curl_exec ($ curl); return $ response; // curl_close ($ curl );} function uploadEntry ($ curl, $ contents, $ auth) {$ document = "Content-Type: text/html/n"; // "Content-Type: application/msword/n "; $ document. = $ contents; $ itemURL =" http://docs.google.com/feeds/default/private/full "; $ Mime. = $ document. "/n -- END_OF_PART --/n"; $ headers = array (); $ headers [] = "POST/feeds/default/private/full HTTP/1.1 "; $ headers [] = "Host: docs.google.com"; $ headers [] = "GData-Version: 3.0"; $ headers [] = "Content-Length :". strlen ($ mime); $ headers [] = "Content-Type: multipart/related; boundary = END_OF_PART"; $ headers [] = "Slug: tmp_example_document.html "; $ headers [] = "Authorization: GoogleLogin auth = ". $ auth; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ itemURL); curl_setopt ($ curl, success, true); curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ mime); $ result = curl_exec ($ curl); // curl_close ($ curl); return $ result ;} function uploadEntry2 ($ curl, $ contents, $ auth, $ file_type = 'Doc') {$ mime = $ contents; // file_get_contents ('f:/g.doc '); // $ headers = array (); $ headers [] = "POST/feeds/documents/private/full HTTP/1.1"; $ headers [] = "Host: docs.google.com "; $ headers [] = "GData-Version: 3.0"; $ headers [] = "Authorization: GoogleLogin auth = ". $ auth. ""; $ headers [] = "Content-Length :". strlen ($ mime); $ headers [] = "Content-Type :". getGoogleType ($ file_type); $ headers [] = "Accept-encoding: identity"; $ headers [] = "Slug: g. {$ file_type} "; // $ headers [] = $ mine; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL ," http://docs.google.com/feeds/default/private/full "); Curl_setopt ($ curl, expires, true); curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ curl, CURLOPT_POST, true); curl_setopt ($ curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ mime); $ result = curl_exec ($ curl); // curl_close ($ curl); return $ result ;} function getTemplate ($ where = array () {global $ db; $ db-> debug = true; $ where [] = "1"; $ where = implode (" And ", $ where); $ rs = $ db-> query (" select * from mail_template where {$ where} "); return $ rs ;} function unicodeEncode (& $ rs) {if (is_array ($ rs) {foreach ($ rs as $ k => $ v) {if (is_array ($ v )) unicodeEncode (& $ rs [$ k]); else {$ rs [$ k] = unicode_encode ($ v) ;}} elseif (is_string ($ rs )) {$ rs = unicode_encode ($ rs);} return $ rs;} // If unicode extension does not existsif (! Function_exists ('unicode _ encode') {/*** unicode_encode a utf8 string ** @ param string $ str * @ return encoded string */function unicode_encode ($ str) {// if (is_integer ($ str) return $ str; $ str = strval ($ str); $ uni_str = ''; for ($ I = 0; $ I <strlen ($ str); $ I ++) {if (ord ($ str [$ I]) <128) {$ uni_str. = $ str [$ I];} else {$ uni_str. = '&#'. utf8_unicode ($ str [$ I]). ';' ;}} return $ uni_str ;} /*** Encode a utf8 chareter to unicode ** @ param char $ c * @ return hex code */function utf8_unicode ($ c) {switch (strlen ($ c )) {case 1: return ord ($ c); case 2: $ n = (ord ($ c [0]) & 0x3f) <6; $ n + = ord ($ c [1]) & 0x3f; return $ n; case 3: $ n = (ord ($ c [0]) & 0x1f) <12; $ n + = (ord ($ c [1]) & 0x3f) <6; $ n + = ord ($ c [2]) & 0x3f; return $ n; case 4: $ n = (ord ($ c [0]) & 0x0f) <18; $ n + = (ord ($ c [1]) & 0x3f) <12; $ n + = (ord ($ c [2]) & 0x3f) <6; $ n + = ord ($ c [3]) & 0x3f; return $ n ;}/ *** Decode a unicode string ** @ param unicode string $ str * @ return decoded string */function unicode_decode ($ str) {$ str = strval ($ str); return preg_replace ("/& # ([/da-f] +);/ie", "chr ($1 )", $ str) ;}} function showForm () {echo '';} $ now = date (" YmdHis "); $ clientlogin_url =" https://www.google.com/accounts/ClientLogin "; $ Clientlogin_post = array (" accountType "=>" GOOGLE "," Email "=>" google username @ gmail.com "," Passwd "=>" google password ", "service" => "writely", "source" => "your application name"); $ curl = curl_init ($ clientlogin_url); $ auth = 'your _ callback '; // $ auth = getAuth ($ curl, $ clientlogin_post);/** export **/$ headers = array ("Authorization: GoogleLogin auth = ". $ auth, "GData-Version: 3.0",); $ content = $ _ POST ['content']; // getTemplate (array ("template_id in (473,517) "); // array (" 'template _ id' = 461 ") // var_dump (unicodeEncode ($ content [0] ['body']); $ content = array ('body' => $ content); foreach (array) $ content as $ v) {// unlink ("g_1_1_v1_'template_id'}"); continue; $ result = uploadEntry2 ($ curl, unicodeEncode ($ v ['body']), $ auth, 'html'); preg_match ("/' http:////docs/.google/.com//feeds//download//documents//Export/?docID= ([A-zA-Z0-9 _/-] +)/I ", $ result, $ matches); $ doc_id = $ matches [1]; $ url =" http://docs.google.com/feeds/download/documents/Export?docID= {$ Doc_id} & exportFormat = pdf "; $ content = exportEntry ($ curl, $ url, $ headers); // var_dump ($ content); var_dump ($ doc_id ); // file_put_contents ("g_00000000now000000000000", $ content); header ('content-type: application/pdf '); echo $ Content; break;}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.