整合到一起

代码整合

from PIL import Image
import os

class do_it(object):
    def __init__(self):
        self.download="./pdf/"
        # 保存目录
    def create_img_pdf(self,file_name,ouput_pdf):
        page = Image.new('RGB', (2480, 3508), 'white')
        image=Image.open(file_name)
        i_w,i_h=image.size
        if i_w>i_h:
            i_n_w=2000
            i_n_h=int(i_h*2000/i_w)
        else:
            i_n_h=3000
            i_n_w=int(i_w*3000/i_h)
        n_img=image.resize((i_n_w,i_n_h))
        page.paste(n_img,(100,100))
        page.save(ouput_pdf,"PDF", resolution=100.0)
    def topdf(self,nfile):
        #保存在当前的pdf文件夹
        # nfile必须独一无二
        com="libreoffice --convert-to pdf:writer_pdf_Export "+nfile+" --outdir "+self.download
        os.system(com)
        name=nfile[nfile.rfind('/')+1:nfile.rfind('.')]+".pdf"
        newa=self.download+name
        return newa
    def combine(self,file_list,out_file):
        file_info=""
        for i in file_list:
            file_info=file_info+" "+i
        com="pdftk "+file_info+" cat output "+out_file
        os.system(com)

合并PDF

def combine(file_list,out_file):
    file_info=""
    for i in file_list:
        file_info=file_info+" "+i
    com="pdftk "+file_info+" cat output "+out_file
    os.system(com)

PDF文件转换 DOC XLS PPPT


# libreoffice --convert-to pdf:writer_pdf_Export --outdir <保存的目录> a.docx

#保存在当前的download文件夹
def topdf(nfile):
    com="libreoffice --convert-to pdf:writer_pdf_Export "+nfile+" --outdir ./download"
    os.system(com)
    name=nfile[nfile.rfind('/')+1:nfile.rfind('.')]+".pdf"
    newa="./download/"+name
    return newa

pillow python 将图片转PDF 单页 300DPI

代码

from PIL import Image
import os

def create_img_pdf(file_name,ouput_pdf):
    page = Image.new('RGB', (2480, 3508), 'white')
    image=Image.open(file_name)
    i_w,i_h=image.size
    if i_w>i_h:
        i_n_w=2000
        i_n_h=int(i_h*2000/i_w)
    else:
        i_n_h=3000
        i_n_w=int(i_w*3000/i_h)
    n_img=image.resize((i_n_w,i_n_h))
    page.paste(n_img,(100,100))
    page.save(ouput_pdf,"PDF", resolution=100.0)

create_img_pdf("./image/2.jpg","op.pdf")

PHP 使用IMAGE 失败

imageMagick 模块的调用,本质是处理图片转为PDF。

<?php
// 图片所在的文件夹路径
$imageFolder = 'path/to/your/images/';
// 输出的 PDF 文件路径
$pdfPath = 'path/to/your/output.pdf';

// 获取文件夹中的所有图片文件
$images = glob($imageFolder . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// 实例化 Imagick 对象
$imagick = new Imagick();

// A4 纸张的尺寸(以像素为单位),假设使用 300 DPI
$a4WidthPx = 2480;  // 8.27 英寸 * 300 DPI
$a4HeightPx = 3508; // 11.69 英寸 * 300 DPI

// 循环加载所有图片,并调整尺寸
foreach ($images as $image) {
    // 实例化一个新的 Imagick 对象用于当前图片
    $imagickImage = new Imagick($image);
    
    // 获取图片的原始尺寸
    $originalWidth = $imagickImage->getImageWidth();
    $originalHeight = $imagickImage->getImageHeight();
    
    // 计算图片的宽高比
    $aspectRatio = $originalWidth / $originalHeight;

    // 根据宽高比和 A4 纸张尺寸计算新的尺寸,确保不超过 A4 纸张大小
    if ($originalWidth / $a4WidthPx > $originalHeight / $a4HeightPx) {
        $newWidth = $a4WidthPx;
        $newHeight = (int)($newWidth / $aspectRatio);
    } else {
        $newHeight = $a4HeightPx;
        $newWidth = (int)($newHeight * $aspectRatio);
    }

    // 调整图片尺寸以适应 A4 纸张大小,保持比例
    $imagickImage->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS, 1);

    // 创建一个 A4 大小的白色图层
    $canvas = new Imagick();
    $canvas->newImage($a4WidthPx, $a4HeightPx, new ImagickPixel('white'));
    
    // 计算图片相对于 A4 纸张的位置(居中)
    $x = ($a4WidthPx - $newWidth) / 2;
    $y = ($a4HeightPx - $newHeight) / 2;

    // 将调整后的图片复制到 A4 画布上
    $canvas->compositeImage($imagickImage, Imagick::COMPOSITE_OVER, $x, $y);
    
    // 将画布添加到 Imagick 集合中
    $imagick->addImage($canvas);
}

// 设置输出格式为 PDF,并设置分辨率为 300 DPI
$imagick->setImageResolution(300, 300);
// 写入图像到文件,合并成一个 PDF
$imagick->writeImages($pdfPath, true);

echo "图片已成功合并为一个 A4 大小的 PDF 文件,分辨率为 300 DPI。";
?>