Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php converts work to pdf

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how php converts work into pdf. It is very detailed and has certain reference value. Friends who are interested must finish it!

Method: 1, open the php.ini file, add the "extension=php_com_dotnet.dll" code, remove the "com.allow_dcom=true" before the ";"; 2, configure office support; 3, use the methods in the PDFConverter class for conversion.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

1. Configure PHP extension

If it is a previous version of PHP5.4, you need to open com.allow_dcom = true in php.ini (that is, remove the previous semicolon).

If it is a later version of PHP5.4, add a line to php.ini to extend extension = php_com_dotnet.dll.

Restart the Apache or IIS server, print the phpinfo () message, and check that the com_dotnet extension is on.

↑ checks whether the com_dotnet.dll file exists in the ext directory of php. If not, please download the corresponding version of dll yourself.

2. Configure office support

OpenOffice is a set of open source cross-platform office software, maintained by many free software people, so that everyone can use free Office in addition to Microsoft Office.

OpenOffice is compatible with Microsoft's office software suite, can convert doc, xls, ppt and other files into PDF format, and its function is absolutely no worse than Microsoft Office.

OpenOffice official website: http://www.openoffice.org/

OpenOffice download: http://www.openoffice.org/download/index.html

OpenOffice requires java support, please make sure that JDK is installed and the JRE environment variable is configured.

1. Configure component services

After the OpenOffice installation is complete, press the win+R shortcut key to enter the run menu, and type Dcomcnfg to open the component services.

[component Services] > > [computer] > > [my computer] > > [DCOM configuration] > > [OpenOffice Service Manager]

Right-click to open the properties panel, select the Security tab, check Custom on startup and activation permissions and access permissions, respectively, and add Everyone permissions.

↑ startup and activation permissions and access permissions use a custom configuration

↑ add Everyone user group, remember to check the name before confirming

The two custom configurations of ↑ are the same, allowing Everyone to have all permissions

Then select the identity tab, check the interactive user, save the settings and exit.

two。 Running software in the background

After installing OpenOffice, you need to start once to confirm that the software is working properly, and then open the command line to run the following command:

Change to the installation directory: cd C:\ Program Files\ OpenOffice 4\ program

Run the software in the background: soffice-headless-accept= "socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard

PS: this command only needs to be executed once to keep the software running in the background, even if the server is restarted.

Third, realize the conversion of files

PDF conversion tool (supports doc, docx, xls, xlsx, ppt, pptx, etc.)

Class PDFConverter {private $com; / * need to install openoffice and run in the background * soffice-headless-accept= "socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard * / public function _ _ construct () {try {$this- > com = new COM ('com.sun.star.ServiceManager') } catch (Exception $e) {die ('Please be sure that OpenOffice.org is installed.');}} / * * Execute PDF file (absolute path) conversion * @ param $source [source file] * @ param $export [export file] * / public function execute ($source, $export) {$source =' file:///'. Str_replace ('\','/', $source); $export = 'file:///'. Str_replace ('\','/', $export); $this- > convertProcess ($source, $export);} / * Get the PDF pages * @ param $pdf_path [absolute path] * @ return int * / public function getPages ($pdf_path) {if (! file_exists ($pdf_path) return 0; if (! is_readable ($pdf_path)) return 0 If ($fp = fopen ($pdf_path,'r')) {$page = 0; while (! feof ($fp)) {$line = fgets ($fp, 255) If (preg_match ('/\ / Count [0-9] + /', $line, $matches) {preg_match ('/ [0-9] + /', $matches [0], $matches2); $page = ($page)

< $matches2[0]) ? $matches2[0] : $page; } } fclose($fp); return $page; } return 0; } private function setProperty($name, $value) { $struct = $this->

Com- > Bridge_GetStruct ('com.sun.star.beans.PropertyValue'); $struct- > Name = $name; $struct- > Value = $value; return $struct;} private function convertProcess ($source, $export) {$desktop_args = array ($this- > setProperty (' Hidden', true)); $desktop = $this- > com- > createInstance ('com.sun.star.frame.Desktop') $export_args = array ($this- > setProperty ('FilterName',' writer_pdf_Export')); $program = $desktop- > loadComponentFromURL ($source,'_ blank', 0, $desktop_args); $program- > storeToURL ($export, $export_args); $program- > close (true);}}

Use PDFConverter (absolute path must be passed in)

$arr = array ('doc',' docx', 'xls',' xlsx', 'ppt',' pptx'); $converter = new PDFConverter (); foreach ($arr as $ext) {$source = _ _ DIR__. '/ office/test.'. Ext; $export = _ _ DIR__. '/ pdf/test.'. $ext. '.pdf'; $converter- > execute ($source, $export); echo'

'. $ext. ' Done

';} these are all the contents of the article "how php converts work to pdf". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report