alexallotment/laravel-google-cloud-print

Laravel 7 的 Google Cloud Print 服务提供商

0.1.2 2017-02-23 17:56 UTC

This package is not auto-updated.

Last update: 2024-10-02 06:07:31 UTC


README

安装

通过 composer

composer require gnurlan/laravel-google-cloud-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 服务设置

创建一个具有 ***@re-speedy-diagnostic.iam.gserviceaccount.com 的服务账户密钥(IAM),并在 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 不是以 http(s):// 开头,则抛出异常

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

  • range 助手通过 ->range($start, $end)(起始页和结束页包含在内)。
  • margins 助手通过 ->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();