DOMPDF 中文:PHP将HTML直接生成PDF

不要花里胡哨,直接跟着大佬的步伐

composer require tekintian/dompdf

大佬的默认中文字体:

膜拜大佬:

https://github.com/tekintian/dompdf

1.HTML文件或者string的HTML变量

2.变成PDF形式

$html="<html><body><h1>这是标题</h1><p>信心斯西南吓死你吸纳asSAA是ASIAJDIASD</p></body></html>";

$dompdf = new Dompdf(); 
//初始化
$dompdf->loadHtml($html);
//读取string信息
// (可选) 设置纸张大小和方向
$dompdf->setPaper('A4', 'portrait');
// 渲染 HTML
$dompdf->render();
$output = $dompdf->output();
//$output为PDF数据

3.使用file_put_contents($save, $output); 保存PDF文件

使用片段

 $html = '<html><body><h1>'.$this->digital_to_chinese($n)." 《".$title."》的附件</h1><ol>";
        foreach ($files as $key => $value) {
            $html=$html."<li>".$value['name']."</li>";
        }
        $html=$html."</ol></body></html>";
        // 创建一个新的 DOMPDF 实例
        $dompdf = new Dompdf();
        // 将 HTML 字符串加载到 DOMPDF
        $dompdf->loadHtml($html);
        // (可选) 设置纸张大小和方向
        $dompdf->setPaper('A4', 'portrait');
        // 渲染 HTML
        $dompdf->render();
        $output = $dompdf->output();
        $save=WEB_ROOT."file/save/".$pdf;
        file_put_contents($save, $output);

读取HTML信息

use Dompdf\Dompdf;
$dompdf = new Dompdf();
// 加载 HTML 文件
$html = file_get_contents('path/to/your/htmlfile.html');
// 设置 HTML 内容
$dompdf->loadHtml($html);
// 设置 PDF 的选项
$dompdf->setPaper('A4', 'portrait');
// 渲染 HTML 为 PDF
$dompdf->render();
 
// 输出PDF信息$pdfContent
$pdfContent = $dompdf->output();
 


//直接设置下载
// 设置响应头
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="output.pdf"');
// 输出 PDF 内容
echo $pdfContent;

Thinkphp 调用PHP文件实例化类 必须反斜线

<?php
namespace app\portal\service;

include "/home/wwwroot/test.test.org.cn/vendor/pdf/fpdf.php";
class PdfService
{
    public function TitleList($title, $files){
        $pdf = new \FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(0, 10, 'Hello World!', 0, 1, 'C');
        $pdf->Output('/home/wwwroot/test.test.org.cn/public/example.pdf', 'F');

    }
}

导入PHP文件 include "/home/wwwroot/test.test.org.cn/vendor/pdf/fpdf.php";

$pdf = new \FPDF(); 实例化类 必须反斜线

MLX 将LORA数据合并生产GGUF

之间的命令可以将LORA导出safetensors文件,这个文件可以直接倒入LM studio,但是OLLAMA 不支持,之前需要安装LLAMA.CPP来将其转化为GGUF格式,但是今天发现MLX自带转换GGUF功能

mlx_lm.fuse \
    --model ../../qwen2.5-0.5B \
    --adapter-path adapters \
    --save-path qwen2.5-0.5B-test_1 \
    --export-gguf

上面命令将导出F16 精度的GGUF文件,默认情况下,GGUF模型会被保存在 fused_model/ggml-model-f16.gguf,但您可以通过 --gguf-path 选项来指定文件名。
只支持float16。,导出的精度只能是flaot16,不能改变精度,无需加任何参数。

#关于--help的打印信息
mlx_lm.fuse --help
Loading pretrained model
usage: mlx_lm.fuse [-h] [--model MODEL] [--save-path SAVE_PATH]
                   [--adapter-path ADAPTER_PATH] [--hf-path HF_PATH]
                   [--upload-repo UPLOAD_REPO] [--de-quantize] [--export-gguf]
                   [--gguf-path GGUF_PATH]

Fuse fine-tuned adapters into the base model.

options:
  -h, --help            show this help message and exit
  --model MODEL         The path to the local model directory or Hugging Face
                        repo.
  --save-path SAVE_PATH
                        The path to save the fused model.
  --adapter-path ADAPTER_PATH
                        Path to the trained adapter weights and config.
  --hf-path HF_PATH     Path to the original Hugging Face model. Required for
                        upload if --model is a local directory.
  --upload-repo UPLOAD_REPO
                        The Hugging Face repo to upload the model to.
  --de-quantize         Generate a de-quantized model.
  --export-gguf         Export model weights in GGUF format.
  --gguf-path GGUF_PATH
                        Path to save the exported GGUF format model weights.
                        Default is ggml-model-f16.gguf.

QWEN不支持MLX转GGUF,只能LLAMA.CPP

MLX转GGUF,只有:GGUF 转换支持限于 fp16 精度的 Mistral、Mixtral 和 Llama 风格的模型