使用mpdf 生成pdf文件 PHP

首先安装mpdf composer require mpdf/mpdf

public function testpdf(Request $request){

        $mpdf = new Mpdf(['tempDir'=>runtime_path().'/pdftmp']);

        $mpdf->autoScriptToLang = true;  
        $mpdf->autoLangToFont = true; 

        $html = <<<EOD
        <h1 style='text-align:center;'>Hello World你好啊</h1>
EOD;

        $mpdf->WriteHTML($html);
        //public_path().'/pdf/'

        $file = public_path().'/pdf/'.time().'.pdf';
        $mpdf->Output($file,'F');

        return response()->download($file, time().'.pdf');
    }

生成结果


杨佳乐 发布于  2024-7-23 23:38 

webman nginx反向代理配置 PHP

upstream webman {
    server 127.0.0.1:8787;
    keepalive 10240;
}

server {
  server_name 站点域名;
  listen 80;
  access_log off;
  root /your/webman/public;

  location ^~ / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      if (!-f $request_filename){
          proxy_pass http://webman;
      }
  }
}

替换server_name 与 root即可

需要注意upstream设置,否则会导致下载大文件中断错误


杨佳乐 发布于  2024-5-13 14:18 

使用webman框架 mysql 有很多sleep连接 PHP

webman使用长链接模式,每个进程都会开启一个mysql连接,不像fpm每次都是连接释放,webman不释放mysql链接,为了防止mysql没有操作而超时,断开连接,会有定时器 定时select 1 在这个期间没有操作数据库,就会显示sleep状态


杨佳乐 发布于  2024-5-7 14:41 

webman-admin修改进入后台默认路由 PHP

默认进入后台为 /app/admin

修改其他复杂入口,例如 /app/abc3ksncvl

项目配置 config/route.php 里添加

use plugin\admin\app\controller\IndexController;

Route::any('/abc3ksncvl', [IndexController::class, 'index']);

杨佳乐 发布于  2024-5-7 14:28 

webman/push 统计在线人数 PHP

自定义进程定时获取在线人数广播给所有在线用户
process/Status.php

<?php

namespace process;

use app\Request;
use Webman\Push\Api;
use Workerman\Timer;
use Workerman\Worker;
use think\facade\Db;

class Status
{
    public function __construct()
    {
        Timer::add(3, function () {
            $this->status();
        });
    }

    public function status()
    {
        static $user_count = 0, $page_count = 0;
        $api = new Api(
            'http://127.0.0.1:3232',
            config('plugin.webman.push.app.app_key'),
            config('plugin.webman.push.app.app_secret')
        );
        $result = $api->get('/channels', ['filter_by_prefix' => 'user', 'info' => 'subscription_count']);
        if (!$result || $result['status'] != 200) {
            return;
        }
        $channels = $result['result']['channels'];
        $user_count_now = count($channels);
        $page_count_now = 0;
        foreach ($channels as $channel) {
            $page_count_now += $channel['subscription_count'];
        }
        if ($page_count_now == $page_count && $user_count_now === $user_count) {
            return;
        }
        $user_count = $user_count_now;
        $page_count = $page_count_now;

        $api->trigger('online-page', 'update_online_status', "当前<b>$user_count</b>人在线,共打开<b>$page_count</b>个页面");
    }
}

自定义进程配置文件
config/process.php

<?php
use Workerman\Worker;
return [
    // ...其它配置...

    'status'  => [
        'handler'  => process\Status::class
    ]
];

前端js代码

var connection = new Push({
    url: 'wss://xxx.com',
    app_key: 'xxxx',
});
function update_online_status(status) {
    $('#online_status').html(status);
}
var online_channel = connection.subscribe('online-page');
online_channel.on('update_online_status', update_online_status);
var user_channel = connection.subscribe('user-<?=session()->getId()?>');
user_channel.on('update_online_status', update_online_status);

以上是实现类似workerman站点底部实时在线统计的代码


杨佳乐 发布于  2024-4-30 14:13