发送HTML或胡须模板,并收到PDF流作为响应。
导航到quay.io/ukhomeofficedigital/html-pdf-converter查找最新的标记版本。 Docker将拉动您指定的任何版本。
例如,如果最新标记的版本v2.4.3,则需要运行此命令:
docker pull quay.io/ukhomeofficedigital/html-pdf-converter:v2.4.3
完成后,您可以通过运行在本地查看图像:
docker image list
所有HOF表格在端口8080上本地运行,在某些情况下也可能正在使用端口8081;因此,HTML-PDF-Converter应在另一个端口上运行。当前建议使用8082端口。
docker run -t -i -p 8082:8080 quay.io/ukhomeofficedigital/html-pdf-converter: ** < tag > **
在终端观察:
2023-09-13T11:18:07.061Z - info: Listening on localhost:8080
注意:终端将说该应用程序在端口8080上侦听,但是您可以通过运行:
docker ps -a
使用HTML到PDF转换器的节点应用也将需要在本地运行。该服务将需要以下ENV变量:
PDF_CONVERTER_URL
:如果您正在运行本地PDF转换器,则该是它正在运行的URL和端口。此URL应为格式PDF_CONVERTER_URL=http://localhost:<PORT>/convert
。
在此示例中,pdf_converter_url将是PDF_CONVERTER_URL=http://localhost:8082/convert
在成功进行PDF转换的HTML时,响应应该看起来像这样:
2023-09-12T15:31:30.249Z - info: status=201, method=POST, url=/convert, response_time=392, content_length=39644
git clone [email protected]:UKHomeOffice/html-pdf-converter.git
cd html-pdf-converter
npm install
npm start
胡子和数据
curl -H " Content-Type:application/json "
-d ' {
"template": " ' "
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{header}}</h1>
<p>{{para}}</p>
</body>
</html>
" ' ",
"data": {
"title": "My title",
"header": "My header",
"para": "My content"
}
} '
-i localhost:8080/convert
html
curl -H " Content-Type:application/json "
-d ' {
"template": " ' "
<html>
<head>
<title>My title</title>
</head>
<body>
<p>Hello world</p>
</body>
</html>
" ' "
} '
-i localhost:8080/convert
响应(示例)
%PDF-1.4 n 1 0 obj n << n/Title ...
Chrome可以接受其PDF渲染功能的许多选项。这些记录在此处:https://github.com/googlechrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
可以通过将pdfOptions
对象作为您请求主体的一部分传递PDFOPTIONS对象,以每次要求设置。
{
"template" : " <h1>Hello World!</h1> " ,
"pdfOptions" : {
"printBackground" : true
}
}
该服务无法解决外部资源,例如链接的CSS,JavaScript或图像。如果您的模板包含指向任何这些资源的链接,我们建议您使用https://github.com/remy/inliner。 Inliner的来源可以是URL,文件位置或HTML字符串。
APP_PORT: Defaults to 8080
APP_HOST: Defaults to ' localhost '
如果本地收到以下错误, html-pdf-converter: Handling error message=Could not find browser revision 756035. Run "npm install" or "yarn install" to download a browser binary.
那么您可能需要手动安装Puppeteer npm i puppeteer