Swift lib for building pdf file data from leaf templates and/or web pages through wkhtmltopdf cli
Built for Vapor 3 and uses Vapor itself (with Swift-NIO under the hood) and Leaf for templates.
I’m not sure that wktmltopdf binary is available in Vapor Cloud, unfortunately.
If you have any info regarding that please feel free to send pull request with instructions to update this readme.
Swift Package Manager
Edit your Package.swift
//add this repo to dependencies
.package(url: "https://github.com/MihaelIsaev/wkhtmltopdf.git", from: "1.1.1")
//and don't forget about targets
//"WKHTMLTOPDF"
Example 🔥
The very common usage is to generate some report on request on-the-fly and return pdf file in response
import WKHTMLTOPDF
router.get("pdf") { req throws -> EventLoopFuture<Response> in
let wk = WK(.topMargin(20),
.leftMargin(20),
.rightMargin(20),
.bottomMargin(20),
.paperSize(.A4))
//Also you can provide path to your `wkhtmltopdf` binary
//while initialization because binary place may be different
//WK(pathToBinary: "/usr/bin/wkhtmltopdf", .topMargin(20), ...)
struct ReportIntro: Codable {
var reportName = "Monthly usage"
}
struct ReportData: Codable {
var records:[ReportRecord] = []
}
let page1 = Page(view: "report-intro", payload: ReportIntro(), params: PageParam.zoom(1.3))
let page2 = Page(view: "report-body", payload: ReportData(), params: PageParam.zoom(1.3))
let page3 = Page<Test>(url: "http://google.com", params: PageParam.zoom(1.3))
return try wk.generate(container: req, pages: page1, page2, page3).map { pdfData in //Data
let response = req.makeResponse(pdfData, as: .pdf)
response.http.headers.add(name: HTTPHeaderName.contentDisposition, value: "attachment; filename=\"report.pdf\"")
return response
}
}
Looks good, right? 😃
Note about UTF-8 encoding
Please make sure that you correctly set html headers in your files and installed all necessary rendering packages in your OS.
Swift lib for building pdf file data from leaf templates and/or web pages through wkhtmltopdf cli Built for Vapor 3 and uses Vapor itself (with Swift-NIO under the hood) and Leaf for templates.
Installation ⚙️
First of all install
wkhtmltopdf
itself.Ubuntu
Either install it automatically
or manually e.g. using
deb
file from latest releasesor manually e.g. using
tar.xz
from latest releasesalso you may need to install some additional packages
macOS
Heroku
Add the WKHTMLTOPDF buildpack to your Heroku app either from the Settings tab on the Heroku dashboard or through the Heroku CLI
When initializing the WK object be sure to specify the “pathToBinary” param as
/app/bin/wkhtmltopdf
:Vapor Cloud
I’m not sure that wktmltopdf binary is available in Vapor Cloud, unfortunately. If you have any info regarding that please feel free to send pull request with instructions to update this readme.
Swift Package Manager
Edit your Package.swift
Example 🔥
The very common usage is to generate some report on request on-the-fly and return pdf file in response
Looks good, right? 😃
Note about UTF-8 encoding
Please make sure that you correctly set html headers in your files and installed all necessary rendering packages in your OS.
e.g. correct html header may look like this
ToDo 👨🏻💻
Cheatsheet 📌
Global Options
It is
GeneralParam
enum type.Uses in
WK
object initialization.Page Options
It is
PageParam
enum type.Uses in
Page
object initialization.Please feel free to contribute!