Code Monkey home page Code Monkey logo

office-convert's Introduction

从doc/docx/xls/xlsx/ppt/pptx转换为pdf

原理

此为使用Windows COM (Component Object Model)方法调用Office相关应用,导出PDF的方法 基本思路就是:调用COM组件,然后模拟 打开/导出/关闭过程

编译 (可跳过)

如果要编译源码,得到exe文件,可以执行命令go build -ldflags "-H windowsgui" 生成 office-convert.exe

运行

普通运行

  • 双击执行 office-convert.exe 即可,但是如果程序报错,或者电脑异常关机,不会重启

后台运行(定时任务启动,可以自动恢复)

  • 1、复制文件

prebuilt下两个文件复制到 C:\Users\Administrator\OfficeConvert\ 目录下

  • 2、修改COM访问权限

当我们以服务、定时任务启动程序的时候,会报错,提示空指针错误。 原因就是微软限制了COM组件在非UI Session的情况下使用(防止恶意病毒之类),如果要允许,需要做如下处理: 参考这里

  • Open Component Services (Start -> Run, type in dcomcnfg)
  • Drill down to Component Services -> Computers -> My Computer and click on DCOM Config
  • Right-click on Microsoft Excel Application and choose Properties
  • In the Identity tab select This User and enter the ID and password of an interactive user account (domain or local) and click Ok

授权

注意,上图是演示,账号密码填写该机器的Administrator账号密码

  • 3、定时任务

创建windows定时任务,每1分钟调用check_start.bat文件,该文件自动检查office-convert.exe是否运行,没有就启动。

注意: 上图只是演示,具体位置填写 C:\Users\Administrator\OfficeConvert\check_start.bat

Web部署/请求/响应

部署

使用nginx作为反向代理,具体位置在 C:\Users\Administrator\nginx-1.20.2\nginx-1.20.2下,修改conf/nginx.conf文件,代理127.0.0.1:10000即可, 有公网IP(比如xxx.com)的话,配置DNS解析convert-tools.xxx.com到此机器ip。

server {
        listen       80;
        server_name  convert-tools.xxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:10000;
        }
        # ...其他设置
}

请求

已部署到Windows机器,访问URL: http://127.0.0.1:10000 (如果上面配置了域名,则访问 http://convert-tools.xxx.com/convert)

请求相关

Method : POST

Content-Type: application/json

Body:

{
    "file_in_url":"https://your_docx_file_url",
    "source_type":"docx",
    "target_type":"pdf"
}
参数 是否必须 取值范围 说明
file_in_url 满足下面source_type的各类文档url 待转换的文档的网络连接
source_type [doc,docx,xls,xlsx,ppt,pptx] 文档类型
target_type pdf 暂时只支持PDF,后续会支持更多

响应

根据HTTP状态码做判断

200 : ok 其他: 有错

Body: 转换的文件的二进制流

如果status_code非200,是对应的报错信息

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.