PHP word、pdf、html之间互相转换-大招在后面

说来话长,接到一个需求,要把word里的内容直接复制到富文本中,样式还要几乎和word中一样,这可愁坏了。尝试了很多扩展均被劝退了。

如:phpoffice/phpword
ncjoes/office-converter

phpword按理来说office 非常可用,但是还要给PHP安装zip扩展,安完后有时还报错,读不出word内容,后来发现它对 doc和docx格式的解析有点问题。

qcloudsms_php 其实是一很好用的扩展,但不知为什么在转文件时会有奇怪的报错,不过我最后解决问题的办法也来自于此,在看了他的源码后有重大发现

正文来了:libreoffice
就是这个工具完全免费,win、linux、mac 都可以安装,能过命令行的方式在多种文件格式中任意转换。

Mac下安装 Libreoffice

Mac 下,可直接从官网下载 Mac 下的软件包进行一键安装。或者用 brew 命令一键安装:

[sudo] brew install libreoffice

检测是否安装成功
在终端执行如下命令,查看版本号,能查到版本号,即表示安装成功。

soffice --version

Ubuntu安装命令

[sudo] apt install libreoffice
apt-get install libreoffice-writer

如果转换后文件存在乱码:

  • 查看字符编码是否支持,比如 gbk 或者 utf-8 等
  • 是否缺少相应的中文字体库。如 ubuntu 下,可以使用如下的方式进行安装:

[sudo] apt install ttf-wqy-zenhei
[sudo] apt install fonts-wqy-microhei

cnetos安装命令

yum install -y libreoffice

Windows下安装Libreoffice

  • 双击下载下来的 Libreoffice 程序,全部使用默认安装的方式进行安装,直接点击“下一步”安装完成。
  • 将Libreoffice 加入到 Windows系统环境 Path 变量。
  • 默认Windows的安装路径是:

C:\Program Files\LibreOffice

加入到系统环境变量中的 Path 变量值:

C:\Program Files\LibreOffice\program

转换命令-其它文档命令

先进入到要转换的文件目录(路径可以自己变换)下面是在mac下执行

word 转 html

soffice --headless --convert-to html ./转换后存储路径 ./被转换文件.doc

html 转 word

soffice --invisible --headless --convert-to docx:"Office Open XML Text" ./被转换文件.html

soffice --headless --invisible --convert-to pdf:writer_pdf_Export D:\logs\xxx.docx --outdir D:\logs

soffice --headless --invisible --convert-to “html:XHTML Writer File:UTF8” D:\logs\xxx.docx --outdir D:\logs

soffice --headless --invisible --convert-to “html:XHTML Writer File:UTF8” D:\logs\xxx.docx --convert-images-to “jpg” --outdir D:\logs

soffice --headless --invisible --convert-to html:impress_html_Export D:\logs\xxx.docx --convert-images-to “jpg” --outdir D:\logs

soffice --headless --invisible --convert-to html D:\logs\xxx.xlsx --outdir D:\logs

soffice --headless --invisible --convert-to pdf:writer_pdf_Export D:\logs\xxx.xlsx --outdir D:\logs

soffice --headless --invisible --convert-to “html:XHTML Writer File:UTF8” D:\logs\xxx-v1.pdf --outdir D:\logs

soffice --headless --invisible --convert-to html D:\logs\test111.xlsx --outdir D:\logs

soffice --headless --invisible --convert-to pdf:writer_pdf_Export D:\logs\测试ppt1.ppt --outdir D:\logs

soffice --headless --invisible --convert-to html “D:\logs\第7章xxx.ppt” --outdir D:\logs

最后的倔强

  • 引入三个包
$ composer require h4cc/wkhtmltopdf-amd64
$ composer require h4cc/wkhtmltoimage-amd64
$ composer require barryvdh/laravel-snappy
  • 安装包成功后,在config/app.php文件中配置service providers和aliaces
'providers' => [
    ....
    Barryvdh\Snappy\ServiceProvider::class,
],
'aliases' => [
    ....
    'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
    'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
]
  • 在config目录生成snappy.php配置文件
$ php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
  • 配置如下:
<?php

return [

    'pdf'   => [
        'enabled' => true,
        'binary'  => "/usr/local/bin/wkhtmltopdf",
        // 'binary'  => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
        'timeout' => false,
        'options' => [],
        'env'     => [],
    ],
    'image' => [
        'enabled' => true,
        'binary'  => "/usr/local/bin/wkhtmltoimage",
        // 'binary'  => base_path('vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64'),
        'timeout' => false,
        'options' => [],
        'env'     => [],
    ],

];

把vendor目录下的命令移动到usr/local/bin下

cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/
  • 完成后执行以下命令尝试运行是否正常
$ wkhtmltopdf -V 

laravel 中使用

$header = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>";
    $html   = file_get_contents(storage_path('app/public/qwe.html'));

    $pdf = App::make('snappy.pdf.wrapper');
    $pdf->loadHTML($header . $html)->setPaper('a4');
    $options = [
        'footer-font-size' => 8,
        'footer-right'     => '[page]',
        'footer-spacing'   => 5,

        'page-size'         => 'A4',
        'minimum-font-size' => 24,

        'margin-top'    => 24,
        'margin-bottom' => 24,
        'margin-left'   => 12,
        'margin-right'  => 12,

        'disable-smart-shrinking' => false,
        'disable-internal-links'  => false,
    ];

    $pdf->setOptions($options)->save(storage_path('app/public/qwe.pdf'));

wkhtmltopdf 有很多参数设置参考

wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>
常规选项
  --allow <path>  允许加载从指定的文件夹中的文件或文件(可重复)
--book*  设置一会打印一本书的时候,通常设置的选项 
  --collate  打印多份副本时整理 
  --cookie <name> <value>  设置一个额外的cookie(可重复) 
  --cookie-jar <path>  读取和写入的Cookie,并在提供的cookie jar文件 
  --copies <number>  复印打印成pdf文件数(默认为1) 
  --cover* <url>  使用HTML文件作为封面。它会带页眉和页脚的TOC之前插入 
  --custom-header <name> <value>  设置一个附加的HTTP头(可重复) 
  --debug-javascript  显示的javascript调试输出 
--default-header*  添加一个缺省的头部,与页面的左边的名称,页面数到右边,例如: --header-left '[webpage]' --header-right '[page]/[toPage]'  --header-line 
  --disable-external-links*  禁止生成链接到远程网页
  --disable-internal-links*  禁止使用本地链接
--disable-javascript  禁止让网页执行JavaScript 
  --disable-pdf-compression*  禁止在PDF对象使用无损压缩 
  --disable-smart-shrinking*  禁止使用WebKit的智能战略收缩,使像素/ DPI比没有不变 
  --disallow-local-file-access  禁止允许转换的本地文件读取其他本地文件,除非explecitily允许用 --allow 
--dpi <dpi>  显式更改DPI(这对基于X11的系统没有任何影响) 
  --enable-plugins  启用已安装的插件(如Flash
  --encoding <encoding>  设置默认的文字编码 
  --extended-help  显示更广泛的帮助,详细介绍了不常见的命令开关 
  --forms*  打开HTML表单字段转换为PDF表单域 
--grayscale  PDF格式将在灰阶产生
--help  Display help 
  --htmldoc  输出程序HTML帮助
  --ignore-load-errors  忽略claimes加载过程中已经遇到了一个错误页面 
--lowquality  产生低品质的PDF/ PS。有用缩小结果文档的空间 
  --manpage  输出程序手册页 
--margin-bottom <unitreal>  设置页面下边距 (default 10mm) 
--margin-left <unitreal>  将左边页边距 (default 10mm) 
--margin-right <unitreal>  设置页面右边距 (default 10mm) 
--margin-top <unitreal>  设置页面上边距 (default 10mm) 
  --minimum-font-size <int>  最小字体大小 (default 5) 
  --no-background  不打印背景
--orientation <orientation>  设置方向为横向或纵向 
  --page-height <unitreal>  页面高度 (default unit millimeter) 
  --page-offset* <offset>  设置起始页码 (default 1) 
--page-size <size>  设置纸张大小: A4, Letter, etc. 
    --page-width <unitreal>  页面宽度 (default unit millimeter) 
  --password <password>  HTTP验证密码 
  --post <name> <value>  Add an additional post field (repeatable) 
  --post-file <name> <path>  Post an aditional file (repeatable) 
  --print-media-type*  使用的打印介质类型,而不是屏幕 
--proxy <proxy>  使用代理 
--quiet  Be less verbose 
--read-args-from-stdin  读取标准输入的命令行参数 
--readme  输出程序自述
--redirect-delay <msec>  等待几毫秒为JS-重定向(default 200) 
--replace* <name> <value>  替换名称,值的页眉和页脚(可重复) 
--stop-slow-scripts  停止运行缓慢的JavaScripts 
--title <text>  生成的PDF文件的标题(第一个文档的标题使用,如果没有指定) 
--toc*  插入的内容的表中的文件的开头
--use-xserver*  使用X服务器(一些插件和其他的东西没有X11可能无法正常工作) 
--user-style-sheet <url>  指定用户的样式表,加载在每一页中
--username <username>  HTTP认证的用户名 
--version  输出版本信息退出
  --zoom <float>  使用这个缩放因子 (default 1) 
 
页眉和页脚选项
--header-center*    <text>    (设置在中心位置的页眉内容)  
--header-font-name* <name>    (default Arial)  (设置页眉的字体名称)
--header-font-size* <size>    (设置页眉的字体大小)
--header-html*  <url> (添加一个HTML页眉,后面是网址)
--header-left*  <text>   (左对齐的页眉文本)
--header-line*      (显示一条线在页眉下)
--header-right* <text>    (右对齐页眉文本)
--header-spacing*   <real>    (设置页眉和内容的距离,默认0)
--footer-center*    <text>    (设置在中心位置的页脚内容)  
--footer-font-name* <name>    (设置页脚的字体名称) 
--footer-font-size* <size>    (设置页脚的字体大小default 11)
--footer-html*  <url> (添加一个HTML页脚,后面是网址)
--footer-left*  <text>    (左对齐的页脚文本)
--footer-line*      显示一条线在页脚内容上)
--footer-right* <text>    (右对齐页脚文本)
--footer-spacing*   <real>    (设置页脚和内容的距离)
./wkhtmltopdf --footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf
./wkhtmltopdf --header-center '报表' --header-line --margin-top 2cm --header-line http://192.168.212.139/oma/  oma.pdf
表内容选项中
 --toc-depth* <level>  Set the depth of the toc (default 3) 
 --toc-disable-back-links*  Do not link from section header to toc 
 --toc-disable-links*  Do not link from toc to sections 
 --toc-font-name* <name>  Set the font used for the toc (default Arial) 
 --toc-header-font-name* <name>  The font of the toc header (if unset use --toc-font-name) 
 --toc-header-font-size* <size>  The font size of the toc header (default 15) 
 --toc-header-text* <text>  The header text of the toc (default Table Of Contents) 
 --toc-l1-font-size* <size>  Set the font size on level 1 of the toc (default 12) 
 --toc-l1-indentation* <num>  Set indentation on level 1 of the toc (default 0) 
 --toc-l2-font-size* <size>  Set the font size on level 2 of the toc (default 10) 
 --toc-l2-indentation* <num>  Set indentation on level 2 of the toc (default 20) 
 --toc-l3-font-size* <size>  Set the font size on level 3 of the toc (default 8) 
 --toc-l3-indentation* <num>  Set indentation on level 3 of the toc (default 40) 
 --toc-l4-font-size* <size>  Set the font size on level 4 of the toc (default 6) 
 --toc-l4-indentation* <num>  Set indentation on level 4 of the toc (default 60) 
 --toc-l5-font-size* <size>  Set the font size on level 5 of the toc (default 4) 
 --toc-l5-indentation* <num>  Set indentation on level 5 of the toc (default 80) 
 --toc-l6-font-size* <size>  Set the font size on level 6 of the toc (default 2) 
 --toc-l6-indentation* <num>  Set indentation on level 6 of the toc (default 100) 
 --toc-l7-font-size* <size>  Set the font size on level 7 of the toc (default 0) 
 --toc-l7-indentation* <num>  Set indentation on level 7 of the toc (default 120) 
 --toc-no-dots*  Do not use dots, in the toc
轮廓选项
 --dump-outline <file>  转储目录到一个文件
 --outline  显示目录(文章中h1,h2来定)
 --outline-depth <level>  设置目录的深度(默认为4)
页脚和页眉
 * [page]       由当前正在打印的页的数目代替
 * [frompage]   由要打印的第一页的数量取代
 * [topage]     由最后一页要打印的数量取代
 * [webpage]    通过正在打印的页面的URL替换
 * [section]    由当前节的名称替换
 * [subsection] 由当前小节的名称替换
 * [date]       由当前日期系统的本地格式取代
 * [time]       由当前时间,系统的本地格式取代
 ./wkhtmltopdf --footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf
 ./wkhtmltopdf --header-center  '报表' --outline  --header-line --margin-top 2cm --header-line http://www.hao123.com/  hao123.pdf
 ./wkhtmltopdf --header-left '[webpage]' --footer-center '测试([page]/[toPage])' http://www.baidu.com baidu.pdf
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容