承接 michaeld555/pdf-converter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michaeld555/pdf-converter

Composer 安装命令:

composer require michaeld555/pdf-converter

包简介

Reliable DOCX to PDF conversion using a local LibreOffice process

README 文档

README

PHP library to convert DOCX files to PDF using a local LibreOffice installation. Conversions run headless with isolated profiles and temporary directories, enabling concurrent executions with minimal conflict.

Requirements

PHP

  • PHP 8.2 or higher.
  • Composer 2.
  • proc_open enabled (used by symfony/process).

LibreOffice

The libreoffice or soffice binary must be installed on the system:

# Ubuntu / Debian
sudo apt-get install --yes libreoffice-writer

# Alpine
apk add --no-cache libreoffice

# macOS
brew install --cask libreoffice

On Windows, install LibreOffice and configure the full path to soffice.exe.

Installation

composer require michaeld555/pdf-converter

Quick start

use Michaeld555\PdfConverter\Converter;

$converter = new Converter();
$converter->convert('/data/input.docx', '/data/output.pdf');

Existing files are not overwritten by default.

Configuration

use Michaeld555\PdfConverter\Converter;
use Michaeld555\PdfConverter\Driver\LibreOfficeConverter;

$driver = new LibreOfficeConverter(
    binary: '/usr/bin/libreoffice',
    timeout: 120.0,
    overwrite: false,
    temporaryDirectory: '/var/tmp/pdf-converter',
);

$converter = new Converter($driver);
$converter->convert('/data/input.docx', '/data/output.pdf');

Driver options

Option Type Default Description
binary string libreoffice Binary name or path.
timeout float 60.0 Max conversion time in seconds.
overwrite bool false Allow overwriting an existing PDF.
processRunner ?ProcessRunner Symfony Runner Advanced integration or testing.
temporaryDirectory ?string System temp Base directory for isolated files.

Error handling

All failures throw Michaeld555\PdfConverter\Exception\ConversionException:

use Michaeld555\PdfConverter\Converter;
use Michaeld555\PdfConverter\Exception\ConversionException;

try {
    (new Converter())->convert('/data/input.docx', '/data/output.pdf');
} catch (ConversionException $exception) {
    error_log($exception->getMessage());
}

Checked scenarios include missing/invalid source or destination, timeout, missing LibreOffice, process errors, and invalid PDF output.

API reference

Michaeld555\PdfConverter\Converter

Main facade. Uses LibreOfficeConverter when no driver is provided.

public function __construct(?DocumentConverter $driver = null)
public function convert(string $source, string $destination): void

Michaeld555\PdfConverter\Driver\LibreOfficeConverter

public function __construct(
    string $binary = 'libreoffice',
    float $timeout = 60.0,
    bool $overwrite = false,
    ?ProcessRunner $processRunner = null,
    ?string $temporaryDirectory = null,
)
public function convert(string $source, string $destination): void

Path rules

  • Source must be a local, readable .docx file (no URLs).
  • Destination must end in .pdf; its parent directory must exist and be writable.
  • Directories are not created automatically.

Custom drivers

Implement DocumentConverter to integrate other mechanisms (Microsoft Graph, internal queue, etc.):

use Michaeld555\PdfConverter\Contract\DocumentConverter;
use Michaeld555\PdfConverter\Converter;

final class CompanyConverter implements DocumentConverter
{
    public function convert(string $source, string $destination): void {}
}

$converter = new Converter(new CompanyConverter());

Docker

FROM php:8.4-cli
RUN apt-get update && apt-get install --yes --no-install-recommends \
        git libreoffice-writer unzip \
    && rm -rf /var/lib/apt/lists/*
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
COPY . .
CMD ["php", "app.php"]

Testing & quality

composer check              # All checks
composer test               # Unit + integration
composer analyse            # PHPStan max level
composer cs:check           # Code style
composer security:audit     # Dependency audit

Integration tests require LibreOffice, pdfinfo (poppler-utils), and the ZIP extension. Use LIBREOFFICE_BINARY env var to set a custom binary for tests.

Troubleshooting

  • LibreOffice not found: Verify proc_open is enabled and set the binary path.
  • Timeout: Increase timeout for large documents or slow storage.
  • Wrong fonts: Install the document fonts and run fc-cache -f.
  • Permission denied: Check read/write access for origin, destination, and temp directories under the PHP process user.
  • File exists: Enable overwrite: true or choose another destination.

Security

  • No documents are sent to third parties.
  • No URL downloading; all input must be local.
  • Process arguments use a list (no shell command injection).
  • Output is validated to start with %PDF- before saving.
  • Temporary files use random names with restricted permissions.

Migration from 1.x

Before After
Converter::docx_to_pdf() static method (new Converter())->convert() instance method
PHP 8.0 minimum PHP 8.2 minimum
Remote URLs accepted Local files only
Destination could be a directory Full path with .pdf required
Overwrite by default Explicit overwrite: true required
FPDI, TCPDF, Office endpoint Removed

The legacy Michaeld555\Converter::docx_to_pdf() adapter is available for gradual migration but is deprecated.

Project structure

src/
├── Contract/   Public interfaces
├── Driver/     Conversion implementations
├── Exception/  Library exceptions
├── Legacy/     Temporary 1.x compatibility
├── Process/    Isolated process execution
└── Converter.php

tests/
├── Integration/
├── Support/
└── Unit/

License

MIT. See LICENSE.

michaeld555/pdf-converter 适用场景与选型建议

michaeld555/pdf-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 205 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「word」 「pdf」 「convert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 michaeld555/pdf-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 michaeld555/pdf-converter 我们能提供哪些服务?
定制开发 / 二次开发

基于 michaeld555/pdf-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 205
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-21