news 2026/5/12 23:56:55

TinyMCE跨平台实现Word图片转存服务器路径配置

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
TinyMCE跨平台实现Word图片转存服务器路径配置

【PHP码农の逆袭】680元预算用PHP硬刚Office文档导入!甲方爸爸直呼“真香”


一、甲方爸爸的“离谱”需求

作为江苏某外包公司“扛把子”PHP程序员,最近接了个CMS官网项目,甲方需求堪称“离谱但合理”:

  1. 核心功能
    • TinyMCE编辑器加按钮,支持Word/Excel/PPT/PDF一键导入,保留字体、颜色、表格、公式(LaTeX/MathType)、形状组,甚至EMZ/WMZ这种“上古”格式。
    • Word粘贴:直接复制Word内容到编辑器,图片自动上传阿里云OSS。
    • 公众号内容导入:甲方说“领导只会从公众号复制,不会排版”。
    • 公式渲染:LaTeX转MathML,多终端(PC/手机/平板/小程序/APP)高清显示。
  2. 技术约束
    • 前端:Vue2 + TinyMCE(甲方说“别升级Vue3,怕兼容性问题”)。
    • 后端:PHP(用Zend Studio开发,甲方说“经典技术栈,安全!”)。
    • 数据库:MySQL(存储文章元数据,图片存OSS)。
    • 服务器:阿里云ECS(公有云+私有云混合部署,甲方说“安全第一”)。
    • 预算:680元(甲方说“这是最后报价,再砍价就找大学生做了”)。
  3. 社交需求
    • 建个QQ群(223813913),新人加群领1~99元红包(钱不多,图个热闹)。
    • 搞代理商机制,推荐客户提20%(比如成交1万,你拿2000元,甲方爸爸的预算就是我的提成了)。

二、开源组件筛选:PHP白嫖党的自我修养
1. TinyMCE插件选型
  • 目标:找个能解析Office文档、自动上传图片到OSS的插件。
  • 候选方案
    • tiny-mce-plugin-powerpaste(商业插件,试用版免费):
      • 优点:支持Word粘贴,图片自动上传,但LaTeX公式支持差。
    • 自定义插件(穷人最终方案)
      • 用TinyMCE的pastefile_picker_callbackAPI,结合PHP处理文档解析和图片上传。
2. 公式处理
  • LaTeX转MathML
    • 前端用MathJax(但多端兼容性差)。
    • 后端用PHP调用Pandoc(通过exec()执行命令行,穷但有效)。
  • MathType公式
    • 识别图片格式(如EMZ/WMZ),用PHP转PNG后上传OSS。
3. 文件导入(Word/Excel/PPT/PDF)
  • 方案
    • PHPWord(解析Word/DOCX)。
    • PHPExcel/PHPOffice(解析Excel)。
    • PHPPresentation(解析PPTX)。
    • TCPDF/FPDI(解析PDF)。
    • 统一处理:解析后提取HTML和图片,图片上传OSS,HTML塞进TinyMCE。

三、开发过程:PHP码农的“暴力”编码
1. 前端:Vue2 + TinyMCE定制插件
  • 步骤1:安装TinyMCE和基础插件:
    npminstall@tinymce/tinymce-vue
  • 步骤2:自定义插件(office-import.js):
    tinymce.PluginManager.add('office-import',function(editor){editor.ui.registry.addButton('office-import',{text:'导入文档',onAction:()=>{constinput=document.createElement('input');input.type='file';input.accept='.docx,.xlsx,.pptx,.pdf';input.onchange=async(e)=>{constfile=e.target.files[0];constformData=newFormData();formData.append('file',file);constres=awaitfetch('/api/import-office.php',{method:'POST',body:formData});constdata=awaitres.json();editor.setContent(data.html);};input.click();}});});
  • 步骤3:在Vue中集成:
2. 后端:PHP处理文档解析和图片上传
  • 图片上传到OSS
    // upload_to_oss.php$base64=$_POST['base64'];$imageData=base64_decode(explode(',',$base64)[1]);$ossClient=newAliyun\OSS\OssClient('endpoint','key','secret');$object='images/'.uniqid().'.png';$ossClient->putObject('bucket',$object,$imageData);echojson_encode(['url'=>"https://bucket.oss-cn-hangzhou.aliyuncs.com/{$object}"]);
  • LaTeX转MathML(Pandoc调用)
    // latex_to_mathml.php$latex=$_POST['latex'];$mathml=shell_exec("echo '{$latex}' | pandoc -f latex -t mathml");echo$mathml;
  • Word解析(PHPWord示例)
    // import_office.phprequire'vendor/autoload.php';$file=$_FILES['file']['tmp_name'];if(pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION)==='docx'){$phpWord=\PhpOffice\PhpWord\IOFactory::load($file);$htmlWriter=new\PhpOffice\PhpWord\Writer\HTML($phpWord);$html=$htmlWriter->getContent();// 提取图片并上传OSS(同上)echojson_encode(['html'=>$html]);}// 其他格式(Excel/PPT/PDF)类似处理
3. 公众号内容处理
  • 前端调用
    // 用户从公众号复制内容后,调用此函数functioncleanWechatContent(html){// 移除公众号冗余标签(如wx-tag)returnhtml.replace(/]*>|<\/wx-tag>/g,'');}
  • 后端处理
    • DOMDocument清理无效标签,保留样式。

四、成果展示:甲方爸爸的“真香”现场
  1. Word粘贴效果
    • 表格、字体、颜色全保留。
    • 图片自动上传OSS,替换为URL。
  2. 公式渲染
    • PC/手机/平板全终端高清显示(MathML的功劳)。
  3. 文件导入
    • Word/Excel/PPT/PDF都能导,样式不乱。

五、恰饭时间:接单群和内推
  1. QQ群:223813913
    • 新人加群领1~99元红包(钱不多,但能买几杯奶茶)。
    • 推荐客户提20%(比如成交1万,你拿2000元,甲方爸爸的预算就是我的提成了)。
  2. 内推需求
    • 兄弟们看看我!会写PHP/Vue/Java,能扛996,求内推!

最后一句暴躁发言

“680元做全功能?甲方爸爸以为我是用爱发电?
(群号再发一遍:223813913,进群送红包,不送是狗!)”

复制插件

安装jquery

npm install jquery

在组件中引入

// 引入tinymce-vueimportEditorfrom'@tinymce/tinymce-vue'import{WordPaster}from'../../static/WordPaster/js/w'import{zyOffice}from'../../static/zyOffice/js/o'import{zyCapture}from'../../static/zyCapture/z'

添加工具栏

//添加导入excel工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).importExcel()}varregister$1=function(editor){editor.ui.registry.addButton('excelimport',{text:'',tooltip:'导入Excel文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('excelimport',{text:'',tooltip:'导入Excel文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('excelimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加word转图片工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().importWordToImg()}varregister$1=function(editor){editor.ui.registry.addButton('importwordtoimg',{text:'',tooltip:'Word转图片',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('importwordtoimg',{text:'',tooltip:'Word转图片',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('importwordtoimg',function(editor){Buttons.register(editor);});}Plugin();}());//添加粘贴网络图片工具栏按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().UploadNetImg()}varregister$1=function(editor){editor.ui.registry.addButton('netpaster',{text:'',tooltip:'网络图片一键上传',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('netpaster',{text:'',tooltip:'网络图片一键上传',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('netpaster',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入PDF按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().ImportPDF()}varregister$1=function(editor){editor.ui.registry.addButton('pdfimport',{text:'',tooltip:'导入pdf文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('pdfimport',{text:'',tooltip:'导入pdf文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('pdfimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入PPT按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor);WordPaster.getInstance().importPPT()}varregister$1=function(editor){editor.ui.registry.addButton('pptimport',{text:'',tooltip:'导入PowerPoint文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('pptimport',{text:'',tooltip:'导入PowerPoint文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('pptimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加导入WORD按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).importWord()}varregister$1=function(editor){editor.ui.registry.addButton('wordimport',{text:'',tooltip:'导入Word文档',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('wordimport',{text:'',tooltip:'导入Word文档',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('wordimport',function(editor){Buttons.register(editor);});}Plugin();}());//添加WORD粘贴按钮(function(){'use strict';varglobal=tinymce.util.Tools.resolve('tinymce.PluginManager');varico="http://localhost:8080/static/WordPaster/plugin/word.png"functionselectLocalImages(editor){WordPaster.getInstance().SetEditor(editor).PasteManual()}varregister$1=function(editor){editor.ui.registry.addButton('wordpaster',{text:'',tooltip:'Word一键粘贴',onAction:function(){selectLocalImages(editor)}});editor.ui.registry.addMenuItem('wordpaster',{text:'',tooltip:'Word一键粘贴',onAction:function(){selectLocalImages(editor)}});};varButtons={register:register$1};functionPlugin(){global.add('wordpaster',function(editor){Buttons.register(editor);});}Plugin();}());

在线代码:

添加插件

// 插件plugins:{type:[String,Array],// default: 'advlist anchor autolink autosave code codesample colorpicker colorpicker contextmenu directionality emoticons fullscreen hr image imagetools importcss insertdatetime link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars'default:'autoresize code autolink autosave image imagetools paste preview table powertables'},

点击查看在线代码

初始化组件

// 初始化WordPaster.getInstance({// 上传接口:http://www.ncmem.com/doc/view.aspx?id=d88b60a2b0204af1ba62fa66288203edPostUrl:'http://localhost:8891/upload.aspx',// 为图片地址增加域名:http://www.ncmem.com/doc/view.aspx?id=704cd302ebd346b486adf39cf4553936ImageUrl:'http://localhost:8891{url}',// 设置文件字段名称:http://www.ncmem.com/doc/view.aspx?id=c3ad06c2ae31454cb418ceb2b8da7c45FileFieldName:'file',// 提取图片地址:http://www.ncmem.com/doc/view.aspx?id=07e3f323d22d4571ad213441ab8530d1ImageMatch:''})

在页面中引入组件

功能演示

编辑器

在编辑器中增加功能按钮

导入Word文档,支持doc,docx

导入Excel文档,支持xls,xlsx

粘贴Word

一键粘贴Word内容,自动上传Word中的图片,保留文字样式。

Word转图片

一键导入Word文件,并将Word文件转换成图片上传到服务器中。

导入PDF

一键导入PDF文件,并将PDF转换成图片上传到服务器中。

导入PPT

一键导入PPT文件,并将PPT转换成图片上传到服务器中。

上传网络图片

一键自动上传网络图片。

下载示例

点击下载完整示例

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 7:22:36

AI智能研修系统:用技术重构高效学习新范式

在数字化学习浪潮中&#xff0c;AI智能研修系统早已不是“高大上”的概念&#xff0c;而是扎根培训场景、用技术破解传统研修痛点的实用工具。它不像科幻电影里的复杂机器&#xff0c;核心是靠三大核心技术&#xff0c;把“千人一面”的培训变成“千人千面”的精准研修&#xf…

作者头像 李华
网站建设 2026/5/10 19:49:47

Dify镜像与主流云服务商GPU资源的对接方案

Dify镜像与主流云服务商GPU资源的对接方案 在企业加速拥抱AI的今天&#xff0c;如何快速构建稳定、高效且可扩展的大模型应用&#xff0c;成为技术团队面临的核心挑战。传统开发方式中&#xff0c;从环境配置到服务部署&#xff0c;再到性能调优&#xff0c;每一步都依赖大量手…

作者头像 李华
网站建设 2026/5/10 3:14:53

8、SharePoint关键设置与分布式缓存管理指南

SharePoint关键设置与分布式缓存管理指南 在SharePoint环境中,良好的构建需要一系列关键设置。本文将深入探讨用户配置文件同步的COM + 安全设置,以及SharePoint 2013和2016的分布式缓存服务的配置、故障排除等内容。 1. 用户配置文件同步的COM + 安全设置 在运行用户配置…

作者头像 李华
网站建设 2026/5/12 15:37:04

17、SharePoint ULS Viewer:高效故障排查利器

SharePoint ULS Viewer:高效故障排查利器 1. ULS Viewer简介 ULS Viewer是一款强大的SharePoint故障排查工具。在GitHub上有两个版本可供选择:版本2.0.3530.27850适用于Windows Server 2008及更早的操作系统;版本16.0.3129.1000则更适合Windows Server 2012及更高版本。 …

作者头像 李华
网站建设 2026/5/11 5:42:04

从零搭建智能自动化流程,清言+Open-AutoGLM实战经验全分享

第一章&#xff1a;从零认识清言浏览器插件(Open-AutoGLM web)清言浏览器插件&#xff08;Open-AutoGLM web&#xff09;是一款基于 AutoGLM 技术的智能化网页交互工具&#xff0c;旨在为用户提供无缝的自然语言操作体验。该插件可嵌入主流浏览器环境&#xff0c;通过语义理解能…

作者头像 李华