iupemai92 / laravel-google-cloud-print
适用于 Laravel 5 的 Google Cloud Print 服务提供商
Requires
- php: >=5.6.0
- google/apiclient: ^2.1
- illuminate/config: 6.x
- illuminate/session: 6.x
- illuminate/support: 6.x
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.6
This package is auto-updated.
Last update: 2024-09-06 20:21:04 UTC
README
安装
通过 composer
composer require bnbwebexpertise/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')
。 - 本地文件通过
->file('/path/to/my/file')
。如果文件不可访问,则会抛出异常 - URL 通过
->url('http://acme.foo/bar')
。在发送打印作业之前,将内容下载到本地。如果 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();