Gotenberg文档转换API客户端:PHP SDK使用教程

Gotenberg文档转换API客户端:PHP SDK使用教程

Gotenberg文档转换API客户端:PHP SDK使用教程

【免费下载链接】gotenberg A developer-friendly API for converting numerous document formats into PDF files, and more! 项目地址: https://gitcode.***/gh_mirrors/go/gotenberg

Gotenberg是一个容器化的文档转换服务,提供友好的API接口,能够将HTML、Markdown、Word、Excel等多种格式的文档转换为PDF。本文将详细介绍如何使用PHP SDK与Gotenberg API进行交互,帮助开发者快速实现文档转换功能。

准备工作

安装Gotenberg服务

在使用PHP SDK之前,需要先在本地或服务器上部署Gotenberg服务。可以通过Docker快速启动:

docker run --rm -p 3000:3000 gotenberg/gotenberg:8

服务启动后,API将在本地的3000端口可用,地址为 http://localhost:3000。

安装PHP SDK

目前Gotenberg官方并未提供PHP SDK,但社区有多个第三方实现。以 thecodingmachine/gotenberg-php-client 为例,可以通过***poser安装:

***poser require thecodingmachine/gotenberg-php-client

基本使用方法

创建客户端实例

首先需要创建一个Gotenberg客户端实例,指定服务地址:

use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\ClientInterface;

$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());

HTML转PDF

下面是一个将HTML内容转换为PDF的示例:

use TheCodingMachine\Gotenberg\ChromiumRequest;
use TheCodingMachine\Gotenberg\DocumentFactory;

$request = new ChromiumRequest(
    DocumentFactory::makeFromHtml('<h1>Hello, Gotenberg!</h1>'),
    'output.pdf'
);

$response = $client->post($request);
file_put_contents('output.pdf', $response->getBody());

Markdown转PDF

Gotenberg同样支持将Markdown转换为PDF:

use TheCodingMachine\Gotenberg\ChromiumRequest;
use TheCodingMachine\Gotenberg\DocumentFactory;

$markdown = '# Hello, Gotenberg!';
$request = new ChromiumRequest(
    DocumentFactory::makeFromMarkdown($markdown),
    'output.pdf'
);

$response = $client->post($request);
file_put_contents('output.pdf', $response->getBody());

高级功能

转换本地文件

除了直接传入内容,还可以转换本地文件:

use TheCodingMachine\Gotenberg\ChromiumRequest;
use TheCodingMachine\Gotenberg\Document;

$document = new Document('/path/to/local/file.html', 'file.html');
$request = new ChromiumRequest($document, 'output.pdf');

$response = $client->post($request);
file_put_contents('output.pdf', $response->getBody());

设置转换选项

可以设置PDF的页面大小、方向、边距等选项:

use TheCodingMachine\Gotenberg\ChromiumRequest;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\Chromium\PageSize;
use TheCodingMachine\Gotenberg\Chromium\Orientation;

$request = new ChromiumRequest(
    DocumentFactory::makeFromHtml('<h1>Hello, Gotenberg!</h1>'),
    'output.pdf'
);

$request->setPageSize(PageSize::A4)
        ->setOrientation(Orientation::LANDSCAPE)
        ->setMarginTop(1.5)
        ->setMarginBottom(1.5)
        ->setMarginLeft(1.5)
        ->setMarginRight(1.5);

$response = $client->post($request);
file_put_contents('output.pdf', $response->getBody());

处理Office文档

Gotenberg还支持将Word、Excel等Office文档转换为PDF,这需要使用LibreOffice模块:

use TheCodingMachine\Gotenberg\LibreOfficeRequest;
use TheCodingMachine\Gotenberg\Document;

$document = new Document('/path/to/document.docx', 'document.docx');
$request = new LibreOfficeRequest($document, 'output.pdf');

$response = $client->post($request);
file_put_contents('output.pdf', $response->getBody());

错误处理

在使用过程中,可能会遇到各种错误,需要进行适当的处理:

try {
    $response = $client->post($request);
    file_put_contents('output.pdf', $response->getBody());
} catch (\TheCodingMachine\Gotenberg\Exception\GotenbergException $e) {
    echo '转换失败: ' . $e->getMessage();
} catch (\Http\Client\Exception $e) {
    echo 'HTTP请求错误: ' . $e->getMessage();
}

总结

通过PHP SDK,我们可以方便地与Gotenberg服务进行交互,实现各种文档格式到PDF的转换。本文介绍了基本的安装配置、常用转换功能以及错误处理方法。更多高级功能和详细参数可以参考Gotenberg的官方文档和PHP SDK的源代码。

使用Gotenberg可以大大简化文档转换的开发工作,让开发者专注于业务逻辑而不是复杂的转换细节。希望本文能够帮助你快速上手Gotenberg PHP SDK的使用。

【免费下载链接】gotenberg A developer-friendly API for converting numerous document formats into PDF files, and more! 项目地址: https://gitcode.***/gh_mirrors/go/gotenberg

转载请说明出处内容投诉
CSS教程网 » Gotenberg文档转换API客户端:PHP SDK使用教程

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买