作者归档:admin

toPDF基于ThinkCMS和libreoffice的在线office转换平台

topdf

github:https://github.com/maysrp/topdf

微信截图_20201016210213.png

准备

office文档在线转换pdf平台
基于ThinkPHP5.1下的ThinkCMS5.1开发,只有word,excel,ppt,txt,文本在线转pdf的功能。

Nginx php>7.1 mysql
libreoffice python3

推荐安装lnmp.org一键安装包,lnmp安装完成后在php中添加fileinfo,之后按照以下命令按照libreoffice和pymysql

apt-get update
apt-get install libreoffice python3-pip
pip3 install pymysql

字体安装

因为libreoffice转码时候需要字体,不然中文会全部乱码
mkdir /usr/share/fonts/Fonts
上传字体到这个目录(/usr/share/fonts/Fonts),字体哪里找?
你的 C:\Windows\Fonts 下面的字体你上传几个就好了,推荐雅黑,宋体,黑体

Nginx

nginx修改fastcgi.conf配置

lnmp下该文件在 /usr/local/nginx/conf/fastcgi.conf

把其中的

#fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/../:/tmp/:/proc/";

Nginx 配置
修好你的nginx的网站配置文件
vim /usr/local/nginx/conf/vhost/你的网站.conf

在root那行,最后添加上/public,如下的代码形式,定义web的根目录为public

root /home/wwwroot/www.yunbt.net/public;

安装

网站安装

下载该项目文件pdfgaoji.tgz到你的网站目录上并且解压,给予data目录 和public/upload , public/download , public/plugins , public/themes , 权限为777
转到你网站的根目录中的python目录下,执行python3 install.py按照提示依次输入数据库用户名,数据库名,数据库密码,即可完成网站的安装。

转码服务端安装

修改python目录下的 run.py和screen.py 中13行代码 设置为你的网站的根目录。
webdir="/home/wwwroot/pdf.gaoji.me/"
run.py使用crontab 去每分钟执行一次去处理文档的转码。
screen.py是使用screen来保持进程,可以做到按秒处理。
推荐使用screen
screen -dmS pdf python3 screen.py

大功告成

后台

微信截图_20201016205614.png

微信截图_20201016205630.png

微信截图_20201016205750.png

office文件的PDF转换

使用libreoffice的命令可以将word,excel,ppt转换为PDF,命令也很简单:
libreoffice --convert-to pdf 输入文件 --outdir 输出文件目录
输入文件可以为word,excel ,ppt之类的office文件,顺道用ThinkCMF写了个在线转换
redPDF

问题还是宽带问题,租不起国内的服务器,因为涉及到上传和下载,说以上下宽带都要大,国内20M的镇江电信普遍都要200+每月,因为涉及到上传word子类的office文件,所以至少10M以上,否则上传等待时间过久。

PHP链接亚马逊S3/MinIO

<?php

// 使用Composer autoloader引入SDK
date_default_timezone_set('America/Los_Angeles');
require 'vendor/autoload.php';

$s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'endpoint' => 'http://localhost:9000',
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => 'YOUR-ACCESSKEYID',
                'secret' => 'YOUR-SECRETACCESSKEY',
            ],
]);


// 发送PutObject请求并获得result对象
$insert = $s3->putObject([
     'Bucket' => 'testbucket',
     'Key'    => 'testkey',
     'Body'   => 'Hello from MinIO!!'
]);

// 下载文件的内容
$retrive = $s3->getObject([
     'Bucket' => 'testbucket',
     'Key'    => 'testkey',
     'SaveAs' => 'testkey_local'
]);

// 通过索引到结果对象来打印结果的body。
echo $retrive['Body'];
<?php
// 从client中获得一个commad对象
$command = $s3->getCommand('GetObject', [
            'Bucket' => 'testbucket',
            'Key'    => 'testkey'
        ]);

// 获得一个10分钟有效期的pre-signed URL
$presignedRequest = $s3->createPresignedRequest($command, '+10 minutes');

// 获得presigned-url
$presignedUrl =  (string)  $presignedRequest->getUri();

查看SDK Amazon S3

https://packagist.org/packages/aws/aws-sdk-php

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html

MINIO Nginx配置 负载均衡

  1. 参考配置
server {
 listen 80;
 server_name example.com;
 location / {
   proxy_set_header Host $http_host;
   proxy_pass http://localhost:9000;
 }
}
  1. 添加存储处理
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m
use_temp_path=off;
server {
    # ...
    location / {
        proxy_cache      my_cache;
        proxy_set_header Host $http_host;
        proxy_pass       http://localhost:9000;
    }
}
  1. loadbalance 配置
 upstream minio {
       server 127.0.0.1:9001 weight=20 max_fails=2 fail_timeout=30s;
       server 127.0.0.1:9002 weight=10 max_fails=2 fail_timeout=30s;
       server 127.0.0.1:9003 weight=10 max_fails=2 fail_timeout=30s;
       server 127.0.0.1:9004 weight=10 max_fails=2 fail_timeout=30s;
    }
location / {
           # proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $http_host;
                client_body_buffer_size 10M;
                client_max_body_size 10G;
                proxy_buffers 1024 4k;
                proxy_read_timeout 300;
                proxy_next_upstream error timeout http_404;
                proxy_pass http://minio;
        }

可以拷贝配置

upstream minio_server {
    #以下4种均衡策略选一个
      # 1、默认轮询负载均衡least_conn;  
    # 2、最少连接负载均衡ip_hash;  
    # 3、会话持久化负载均衡
      # 4、带权重的负载均衡 
    server 192.168.8.110:9000 weight=3;
    server 192.168.8.110:9000;
    server 192.168.8.111:9000;
    server 192.168.8.112:9000;
    server 192.168.8.113:9000;
    }

server {
    listen 80;
    server_name example.com;
    #注意所有类型的location仅仅检验请求行(HTTP中的request line)中的URL部分,不带参数。这是因为请求字符串中的参数可以以多种方式给出
    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://minio_server;
    }
}