Google php convert docx to pdf

Convert Word doc, docx and Excel xls, xlsx to PDF with PHP

I am looking for a way to convert Word and Excel files to PDF using PHP. The reason for this, is I need to be able to combine files of various formats into one document. I know that if I am able to convert everything to PDF I can then merge the PDFs into one file using PDFMerger (which uses fpdf). I am already able to create PDFs from other file types / images, but am stuck with Word Docs. (I think I would possibly be able to convert the Excel files using the PHPExcel library that I already use to create Excel files from html code). I do not use the Zend Framework, so am hoping that someone will be able to point me in the right direction. Alternatively, if there is a way to create image (jpg) files from the Word documents, that would be workable.

Sadly, because of the data that is being stored, the files have to remain stored securely on the server and can not be transferred to google’s servers.

12 Answers 12

I found a solution to my issue and after a request, will post it here to help others. Apologies if I missed any details, it’s been a while since I worked on this solution.

Читайте также:  Create expo app typescript

The first thing that is required is to install Openoffice.org on the server. I requested my hosting provider to install the open office RPM on my VPS. This can be done through WHM directly.

Now that the server has the capability to handle MS Office files you are able to convert the files by executing command line instructions via PHP. To handle this, I found PyODConverter: https://github.com/mirkonasato/pyodconverter

I created a directory on the server and placed the PyODConverter python file within it. I also created a plain text file above the web root (I named it «adocpdf»), with the following command line instructions in it:

directory=$1 filename=$2 extension=$3 SERVICE='soffice' if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then unset DISPLAY /usr/bin/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & sleep 5s fi python /home/website/python/DocumentConverter.py /home/website/$directory$filename$extension /home/website/$directory$filename.pdf 

This checks that the openoffice.org libraries are running and then calls the PyODConverter script to process the file and output it as a PDF. The 3 variables on the first three lines are provided when the script is executed from with a PHP file. The delay («sleep 5s») is used to ensure that openoffice.org has enough to time to initiate if required. I have used this for months now and the 5s gap seems to give enough breathing room.

The script will create a PDF version of the document in the same directory as the original.

Finally, initiating the conversion of a Word / Excel file from within PHP (I have it within a function that checks if the file we are dealing with is a word / excel document).

//use openoffice.org $output = array(); $return_var = 0; exec("/opt/adocpdf  ", $output, $return_var); 

This PHP function is called once the Word / Excel file has been uploaded to the server. The 3 variables in the exec() call relate directly to the 3 at the start of the plain text script above. Note that the $directory variable requires no leading forward slash if the file for conversion is within the web root.

OK, that’s it! Hopefully this will be useful to someone and save them the difficulties and learning curve I faced.

Источник

doc, docx conversion to pdf using php

i’ve been searching for quite a long now to convert a word document (.doc & .docx ) to pdf. my application is about taking a word document from clients than converting them to a pdf with added changes ( like header, footer ) to the original document. Any suggestions are welcomed. Thank you

You might want to go for a solution which uses some office program remote controlled, be it ms office, open office, or any other comparable product. Rendering doc files e.g. to PDF is fairly complex, but office products bring along all required capabilities.

Thank you for the suggestion. It would be helpful if you can provide some example link in here. Thanks ones again.

I had done some office program remote control from Java some years ago, not from php, though. It proved to be quite workable. Thus, i can not present current php code solving your issue, only the positive experience that it can be done and generates good output.

4 Answers 4

Look at http://www.phplivedocx.org/ for converting DOC to PDF.

Thanks you, i’ve tried using phpdocx but my generated PDF din’t had the images as well as text formatting same as original doc.

You can call the Docmosis Cloud Services from PHP using a http post or curl command. It supports doc and docx imports and pdf outputs. It’s more intended to perform document manipulations than conversions but perhaps that suits you even better. Please note I work for the company that created Docmosis.

This is a year old, but for people still looking I found a link to a pretty decent api you can look at and see if it works for your needs.

Essentially you use their api and a key they provide when you sign up for an account. Send all the data for conversions to their servers then get the return. You can then store it on your own computer, the users computer, or a server of your choosing. There is some minimal set up and sign up processes and their Github account walks you through pretty easily.

Check it out and hopefully it is a good choice.

Источник

how to convert .docx / .doc file to .pdf using php only

I want to convert ms-word file(.doc,.docx) to .pdf or .html file without losing style and image on word file.

 $filenamearray = explode('.',$filename ); $doc = new Docx_reader(); $doc->setFile($item); if(!$doc->get_errors()) < $html = $doc->to_html(); $plain_text = $doc->to_plain_text(); $myfile = file_put_contents("$filenamearray[0].html", $html.PHP_EOL , FILE_APPEND | LOCK_EX); > else < echo implode(', ',$doc->get_errors()); > 

Above solution i tried but giving me only html without style and image.i want .html or .pdf same as .doc .docx file.

1 Answer 1

This is not an easy task and your rate of success will largely depend on the complexity of your word document. If only basic style elements are used (bold, italic, underline, color) you could use the HTML add on to FPDF. Any more complex elements would require a translation function to FPDF.

I assume you need this for template purposes, may I suggest directly programming your page layout in FPDF. You can create custom tags for layout which can be used in a WYSIWYG editor.

You will lose some of the flexibility compaired to a Word document, but that loss does not outweight the ease of setting up standard FPDF. But again, your best way to go will depend on the functionality you need and your end-goal. I suggest taking a look at the possibilities with FPDF. It’s a great script and can make PDF’s lightning fast!

Источник

Google Api for PHP (Drive API) Export as .pdf uploaded .docx file

I cannot obtain stable script when I try upload docx file to Google Drive, and then download that file but as PDF. Code:

//Google API require_once('vendor/autoload.php'); putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/2ab4ece19bd5.json'); $client = new Google_Client(); $client->setApplicationName('sp-gen'); $client->setScopes(array('https://www.googleapis.com/auth/drive')); $client->useApplicationDefaultCredentials(); $service = new Google_Service_Drive($client); $fileMetadata = new Google_Service_Drive_DriveFile(array( 'name' => '281e2399740c88957143507721bd0f25.docx', 'mimeType' => 'application/vnd.google-apps.document' )); $content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); $file = $service->files->create($fileMetadata, array( 'data' => $content, 'mimeType' => 'application/vnd.google-apps.document', 'uploadType' => 'multipart', 'fields' => 'id') ); $content = $service->files->export($file->id, 'application/pdf', array( 'alt' => 'media' )); file_put_contents(str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'), $content->getBody()->getContents()); 

This code works in.. 20-30% of uses. Sometimes, $service->files->export() return error code 500 but in many cases request return normal response (200) but with Content-Length 0. Am I doing something wrong? Or should I do some kind of loop, that try download file until success?

$service->files->create() return object with id field contains ID of file created on Google Drive. Except error code 500 $file->id always exists.

I tried 1-3 sec delay and I have not noticed any improvement. Mayby delay was too small. I will check that.

1 Answer 1

OK. So I spend two days looking for solution and I came up with several conclusions:

  1. Using Server-to-Server authentication, you must create service account, with is «separate» google drive account. That’s means that if you create file via script, file is created on service account, and you can access this file only via API.
  2. You can assign permissions to created file, that connect your real google account to file, and than you can access that file via Google Drive. You can not transfer ownership to file from service account to google account, because Google PHP API at this moment do not have proper method. Or I do not see way to setup this.Class summary.
  3. Key is to use Exponential backoff. In other words try unless success. ( ͡° ͜ʖ ͡°)
//Google API require_once('vendor/autoload.php'); putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/2ab4ece19bd5.json'); $client = new Google_Client(); $client->setApplicationName('sp-gen'); $client->setScopes(array('https://www.googleapis.com/auth/drive')); $client->useApplicationDefaultCredentials(); $service = new Google_Service_Drive($client); $fileMetadata = new Google_Service_Drive_DriveFile(array( 'name' => '281e2399740c88957143507721bd0f25.docx', 'mimeType' => 'application/vnd.google-apps.document' )); $content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); $file = $service->files->create($fileMetadata, array( 'data' => $content, 'uploadType' => 'multipart' ); //Create new permission $newPermission = new Google_Service_Drive_Permission(); //set email of account, that will have access to file. $newPermission->setEmailAddress(''); //Must be user or group, if you pass email adress // user | group | domain | anyone $newPermission->setType('user'); //Could be owner but I can not set transferOwnership // organizer | owner | reader | writer $newPermission->setRole('writer'); $service->permissions->create($file->getId(), $newPermission); $file_name = str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'); $attempt = 1; do< //Wait 5000ms usleep(500000*$attempt); //Try to get pdf file. $content = $service->files->export($file->getId(), 'application/pdf', array( 'alt' => 'media' )); //Save just fetched data. file_put_contents($file_name, $content->getBody()->getContents()); if(filesize($file_name)) break; else $attempt++; >while(true); 

Источник

Оцените статью