aliworkshop/laravel-google-cloud-print

Laravel 6,7 的 Google Cloud Print 服务提供商

1.0.2 2020-04-14 10:49 UTC

This package is auto-updated.

Last update: 2024-09-14 21:43:41 UTC


README

安装

通过 composer

composer require aliworkshop/laravel-google-print

然后添加服务提供器类到你的 Laravel config/app.php

'providers' => [
    // ...
    Bnb\GoogleCloudPrint\LaravelServiceProvider::class,
    // ...
],

如果你打算使用它,也可以添加 Facade 别名

'aliases' => [
    // ...
    'GoogleCloudPrint' => Bnb\GoogleCloudPrint\Facades\GoogleCloudPrint::class,
    // ...
],

配置

将环境参数 GCP_CREDENTIALS_PATH 设置为从 Google Console 下载的服务帐户 JSON 文件的绝对路径(或相对于 Laravel 应用程序根目录的相对路径)。

Google 服务设置

创建一个服务帐户密钥(IAM),使用 ***@re-speedy-diagnostic.iam.gserviceaccount.com,并在 https://console.developers.google.com/apis/credentials 下载 JSON 密钥文件。将文件复制到配置的环境路径项目。

你还需要通过 Google Cloud Print 控制台在 https://www.google.com/cloudprint/#printers 上允许所有所需打印机的打印访问。

如果 Google API 拒绝了凭据,此库将尝试接受邀请。确实,Google 服务帐户不会收到带有接受链接的邀请电子邮件,因此需要使用 API 来完成此过程。

用法

创建打印任务

可以使用 Facade 或使用提供的三个内容类型之一通过快捷方式获取打印任务对象

$task = GoogleCloudPrint::asText()
$task = GoogleCloudPrint::asHtml()
$task = GoogleCloudPrint::asPdf()

// or

$task = app('google.print')->asText()
$task = app('google.print')->asHtml()
$task = app('google.print')->asPdf()

配置并发送打印任务

调用 ->printer($printerId) 是必需的。$printerId 是您在 Google Cloud Print 控制台打印机详细信息页面(或在打印机 URL)上获得的打印机的 UUID。

内容可以以三种方式提供

  • 通过 ->content('A raw content') 提供 raw。
  • 通过 ->file('/path/to/my/file') 提供本地文件。如果文件不可访问,则抛出异常
  • 通过 ->url('http://acme.foo/bar') 提供 URL。在发送打印作业之前,将内容下载到本地。如果 URL 不是以 http(s):// 开头,则抛出异常

您可以通过 ->ticket($key, $value) 方法设置任何其他 Cloud Job Ticket 选项。提供了一些辅助工具

  • 范围辅助工具通过 ->range($start, $end)(包括起始页和结束页)。
  • 边距辅助工具通过 ->marginsInMillimeters($top, $right, $bottom, $left)->marginsInCentimeters($top, $right, $bottom, $left)

如果作业被拒绝,则抛出异常。

示例

$printerId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

// Printing HTML from an URL
GoogleCloudPrint::asHtml()
    ->url('https://open-source.org.cn/licenses/MIT')
    ->printer($printerId)
    ->send();

// Printing page 3 to 10 of a PDF from a local file
GoogleCloudPrint::asPdf()
    ->file('storage/document.pdf')
    ->range(3, 10)
    ->printer($printerId)
    ->send();

// Printing plain text with a 1cm margin on each sides using
GoogleCloudPrint::asText()
    ->content('This is a test')
    ->printer($printerId)
    ->marginsInCentimeters(1, 1, 1, 1)
    ->send();