Php doc file pdf

Convert Doc/Docx Files to Pdf Using PHP

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

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.

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.

Читайте также:  Python syntaxerror non utf 8 code starting with xd0 in file

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.

Convert Doc/Docx Files To PDF using PHP

Open Office and This Code Helps me 🙂

 set_time_limit(0); 
function MakePropertyValue($name,$value,$osm) $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
>
function word2pdf($doc_url, $output_url) //echo $output_url;
//Invoke the OpenOffice.org service manager
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
//Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
//Launch the desktop
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
//Load the .doc file, and pass in the "Hidden" property from above
$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
//Set up the arguments for the PDF output
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
//print_r($export_args);
//Write out the PDF
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
>
$output_dir = "C:/wamp/www/vineesh/";
$doc_file = "C:/wamp/www/vineesh/vineesh.docx";
$pdf_file = "vineesh.pdf";
$output_file = $output_dir . $pdf_file;
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
word2pdf($doc_file,$output_file);
?>

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

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!

Convert docx to pdf using PHP

Open the generated docx with PHPDOCX http://www.phpdocx.com/

 require_once 'phpdocx_pro/classes/TransformDoc.inc'; 
$docx = new TransformDoc();
$docx->setStrFile('document.docx');
$docx->generateXHTML();
$html = $docx->getStrXHTML();

Also, you can export the docx to PDF with

Note this is not a free library

doc,docx file into pdf using shell_exec

simplest wy is to use «unoconv»:

sudo apt-get install unoconv
doc2pdf respondus-docx-sample-file.docx

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Create PDF formatted documentation for your PHP projects

License

lluiscamino/phpDoc2pdf

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Scrutinizer Code Quality Build Status

Create PDF formatted documentation for your PHP projects.

phpDoc2pdf uses the phpDocumentor/Reflection library to extract the classes, interfaces and traits from your project and generate a set of documentation similar to the one that phpDocumentor2 provides, but in PDF format.

You can see some examples on the docs folder.

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Use the following command to generate the documentation:

php phpDoc2pdf.phar create INPUT> OUTPUT>

Replace with the PHP file or directory that you want to document and with the directory where the documentation will be saved.

Please note that the files you want to document have to be in UTF-8 format.

All contributions are welcome, please see the To-do list below or suggest something. 🤗

  • Add tests
  • Add markdown support
  • Add templates system
  • Separate generated docs into different subdirectories
  • Make installation and usage simpler
  • Include phpDocBlock types on method arguments
  • Add support for all type of file extensions.

This project is licensed under the MIT License — see the LICENSE file for details

About

Create PDF formatted documentation for your PHP projects

Источник

How to Convert DOCX to PDF Using PHP

Vyom Srivastava

Illustration: How to Convert DOCX to PDF Using PHP

In this post, you’ll learn how to convert DOCX files to PDFs using PSPDFKit’s DOCX to PDF PHP API. With our API, you can convert up to 100 PDF files per month for free. All you need to do is create a free account to get access to your API key.

PSPDFKit API

Document conversion is just one of our 30+ PDF API tools. You can combine our conversion tool with other tools to create complex document processing workflows. You’ll be able to convert various file formats into PDFs and then:

  • Merge several resulting PDFs into one
  • OCR, watermark, or flatten PDFs
  • Remove or duplicate specific PDF pages

Once you create your account, you’ll be able to access all our PDF API tools.

Step 1 — Creating a Free Account on PSPDFKit

Go to our website, where you’ll see the page below, prompting you to create your free account.

Free account PSPDFKit API

Once you’ve created your account, you’ll be welcomed by the page below, which shows an overview of your plan details.

Free plan PSPDFKit API

As you can see in the bottom-left corner, you’ll start with 100 documents to process, and you’ll be able to access all our PDF API tools.

Step 2 — Obtaining the API Key

After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following page, which is an overview of your keys:

Convert DOCX to PDF PHP API Key

Copy the Live API Key, because you’ll need this for the DOCX to PDF API.

Step 3 — Setting Up Folders and Files

Now, create a folder called docx_to_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside docx_to_pdf and name them input_documents and processed_documents .

Next, copy your DOCX file to the input_documents folder and rename it to document.docx . You can use our demo document as an example.

Then, in the root folder, docx_to_pdf , create a file called processor.php . This is the file where you’ll keep your code.

Your folder structure will look like this:

docx_to_pdf ├── input_documents | └── document.docx ├── processed_documents └── processor.php

Step 4 — Writing the Code

Open the processor.php file and paste the code below into it:

 $FileHandle = fopen('processed_documents/result.pdf', 'w+'); $curl = curl_init(); $instructions = ' < "parts": [ < "file": "document" >] >'; curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.pspdfkit.com/build', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => array( 'instructions' => $instructions, 'document' => new CURLFILE('input_documents/document.docx') ), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer YOUR API KEY HERE' ), CURLOPT_FILE => $FileHandle, )); $response = curl_exec($curl); curl_close($curl); fclose($FileHandle);

ℹ️ Note: Make sure to replace YOUR_API_KEY_HERE with your API key.

Code Explanation

In the code above, you create a FileHandle variable that will allow you to save the file in the processed_documents folder.

Then, you create the instructions variable, where all the instructions for the API will be stored in the form of a JSON string. Finally, you make a CURL request to process the target file.

Output

To execute the code, run the command below:

On successful execution, you’ll see a new processed file, result.pdf , located in the processed_documents folder.

The folder structure will look like this:

docx_to_pdf ├── input_documents | └── document.docx ├── processed_documents | └── result.pdf └── processor.php

Final Words

In this post, you learned how to easily and seamlessly convert DOCX files to PDF documents for your PHP application using our DOCX to PDF PHP API.

You can integrate all of these functions into your existing applications. With the same API token, you can also perform other operations, such as merging several documents into a single PDF, adding watermarks, and more. To get started with a free trial, sign up here.

Источник

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