Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐘 A PHP client for interacting with Gotenberg.

License

NotificationsYou must be signed in to change notification settings

gotenberg/gotenberg-php

Repository files navigation

Gotenberg PHP Logo

Gotenberg PHP

A PHP client for interacting with Gotenberg

Latest VersionTotal DownloadsMonthly DownloadsContinuous Integrationhttps://codecov.io/gh/gotenberg/gotenberg


This package is a PHP client forGotenberg, a developer-friendly API to interact with powerfultools like Chromium and LibreOffice for converting numerous document formats (HTML, Markdown, Word, Excel, etc.) intoPDF files, and more!

Gotenberg versionClient
8.x(current)v2.x(current)
7.xv1.x
6.xthecodingmachine/gotenberg-php-client

Tip

ASymfony Bundle is also available!

Quick Examples

You may convert a target URL to PDF and save it to a given directory:

useGotenberg\Gotenberg;// Converts a target URL to PDF and saves it to a given directory.$filename = Gotenberg::save(    Gotenberg::chromium($apiUrl)->pdf()->url('https://my.url'),$pathToSavingDirectory);

You may also convert Office documents:

useGotenberg\Gotenberg;useGotenberg\Stream;// Converts Office documents to PDF.$response = Gotenberg::send(    Gotenberg::libreOffice($apiUrl)        ->convert(            Stream::path($pathToDocx),            Stream::path($pathToXlsx)        ));

Requirement

This packages requiresGotenberg, a containerized API for seamless PDF conversion.

See theinstallation guide for more information.

Installation

This package can be installed with Composer:

composer require gotenberg/gotenberg-php

We usePSR-7 HTTP message interfaces (i.e.,RequestInterface andResponseInterface) and thePSR-18 HTTP clientinterface (i.e.,ClientInterface).

For the latter, you may need an adapter in order to use your favorite client library. Check the available adapters:

If you're not sure which adapter you should use, consider using thephp-http/guzzle7-adapter:

composer require php-http/guzzle7-adapter

Build a request

This package is organized aroundmodules, namely:

useGotenberg\Gotenberg;Gotenberg::chromium($apiUrl);Gotenberg::libreOffice($apiUrl);Gotenberg::pdfEngines($apiUrl);

Each of these modules offers a variety of methods to populate amultipart/form-data request.

After setting all optional form fields and files, you can create a request by calling the method that represents the endpoint.For example, to call the/forms/chromium/convert/url route:

useGotenberg\Gotenberg;Gotenberg::chromium($apiUrl)    ->pdf()// Or screenshot().    ->singlePage()// Optional.    ->url('https://my.url'));

Tip

Head to thedocumentation to learn about all possibilities.

If the route requires form files, use theStream class to create them:

useGotenberg\DownloadFrom;useGotenberg\Gotenberg;useGotenberg\Stream;Gotenberg::libreOffice($apiUrl)    ->convert(Stream::path($pathToDocument));// Alternatively, you may also set the content directly.Gotenberg::chromium($apiUrl)    ->pdf()    ->assets(Stream::string('style.css','body{font-family: Arial, Helvetica, sans-serif;}'))    ->html(Stream::string('index.html','<html><head><link rel="stylesheet" type="text/css" href="style.css"></head><body><p>Hello, world!</p></body></html>'));// Or create your stream from scratch.Gotenberg::libreOffice($apiUrl)    ->convert(newStream('document.docx',$stream));// Or even tell Gotenberg to download the files for you.Gotenberg::libreOffice($apiUrl)    ->downloadFrom([newDownloadFrom('https://url.to.document.docx', ['MyHeader' =>'MyValue'])    ])    ->convert();

Send a request to the API

After having created the HTTP request, you have two options:

  1. Get the response from the API and handle it according to your need.
  2. Save the resulting file to a given directory.

Get a response

You may use any HTTP client that is able to handle aPSR-7RequestInterface to call the API:

useGotenberg\Gotenberg;$request = Gotenberg::chromium($apiUrl)    ->pdf()    ->url('https://my.url');$response =$client->sendRequest($request);

If you have aPSR-18 compatible HTTP client (seeInstallation), you may also useGotenberg::send:

useGotenberg\Gotenberg;$request = Gotenberg::chromium($apiUrl)    ->pdf()    ->url('https://my.url');try {$response = Gotenberg::send($request);return$response;}catch (GotenbergApiErrored$e) {// $e->getResponse();}

This helper will parse the response and if it is not2xx, it will throw an exception. That's especially useful ifyou wish to return the response directly to the browser.

You may also explicitly set the HTTP client:

useGotenberg\Gotenberg;$response = Gotenberg::send($request,$client);

Save the resulting file

If you have aPSR-18 compatible HTTP client (seeInstallation), you may useGotenberg::save:

useGotenberg\Gotenberg;$request = Gotenberg::chromium($apiUrl)    ->pdf()    ->url('https://my.url');$filename = Gotenberg::save($request,'/path/to/saving/directory');

It returns the filename of the resulting file. By default, Gotenberg creates aUUID filename (i.e.,95cd9945-484f-4f89-8bdb-23dbdd0bdea9) with either a.zip or a.pdf file extension (or image formats for screenshots).

You may also explicitly set the HTTP client:

useGotenberg\Gotenberg;$response = Gotenberg::save($request,$pathToSavingDirectory,$client);

Filename

You may override the output filename with:

useGotenberg\Gotenberg;$request = Gotenberg::chromium($apiUrl)    ->pdf()    ->outputFilename('my_file')    ->url('https://my.url');

Gotenberg will automatically add the correct file extension.

Trace or request ID

By default, Gotenberg creates aUUID trace that identifies a request in its logs. You may override its value thanks to:

useGotenberg\Gotenberg;$request = Gotenberg::chromium('$apiUrl')    ->pdf()    ->trace('debug')    ->url('https://my.url');

It will set the headerGotenberg-Trace with your value. You may also override the default header name:

useGotenberg\Gotenberg;$request = Gotenberg::chromium($apiUrl)    ->pdf()    ->trace('debug','Request-Id')    ->url('https://my.url');

Please note that it should be the same value as defined by the--api-trace-header Gotenberg's property.

The response from Gotenberg will also contain the trace header. In case of error, both theGotenberg::send andGotenberg::save methods throw aGotenbergApiErrored exception that provides the following method for retrieving thetrace:

useGotenberg\Exceptions\GotenbergApiErrored;useGotenberg\Gotenberg;try {$response = Gotenberg::send(        Gotenberg::chromium($apiUrl)            ->screenshot()            ->url('https://my.url')    );}catch (GotenbergApiErrored$e) {$trace =$e->getGotenbergTrace();// Or if you override the header name:$trace =$e->getGotenbergTrace('Request-Id');}

Sponsor this project

 

Contributors13


[8]ページ先頭

©2009-2026 Movatter.jp