發送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