Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix threadpool usage and update README #10

Merged
merged 2 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# wkhtmltopdf

![Swift](http://img.shields.io/badge/swift-4.2-brightgreen.svg)
![Vapor](http://img.shields.io/badge/vapor-3.0-brightgreen.svg)
![Swift](http://img.shields.io/badge/swift-5.2-brightgreen.svg)
![Vapor](http://img.shields.io/badge/vapor-4.0-brightgreen.svg)
![Travis](https://travis-ci.org/vapor-community/wkhtmltopdf.svg?branch=master)

Vapor 3 library for converting HTML (Leaf or otherwise) into PDF files using
Vapor 4 library for converting HTML (Leaf or otherwise) into PDF files using
[wkhtmltopdf](http://wkhtmltopdf.org/).

## Getting Started

Add the following in your `Package.swift` file
```Swift
.package(url: "https://github.com/vapor-community/wkhtmltopdf.git", from: "2.0.0"),
.package(url: "https://github.com/vapor-community/wkhtmltopdf.git", from: "3.0.0"),
```

## 📘 Overview

First, install [wkhtmltopdf](http://wkhtmltopdf.org/downloads.html). This
library is tested on version 0.12.4. Specify the location of `wkhtmltopdf`
library is tested on version 0.12.5. Specify the location of `wkhtmltopdf`
in the `Document` initialiser. The default is `/usr/local/bin/wkhtmltopdf`.
Run it to ensure it and any dependencies are installed correctly.

Expand Down Expand Up @@ -49,14 +49,14 @@ func pdf(_ req: Request) -> Future<Response> {
// Add the pages to the document
document.pages = [page1] + pages
// Render to a PDF
let pdf = try document.generatePDF(on: req)
let pdf = try document.generatePDF(eventLoop: req.eventLoop)
// Now you can return the PDF as a response, if you want
return pdf.map { data -> Response in
let http = HTTPResponse(status: .ok,
headers: HTTPHeaders([("Content-Type", "application/pdf")]),
body: data)
return Response(http: http,
using: req)
return HTTPResponse(
status: .ok,
headers: HTTPHeaders([("Content-Type", "application/pdf")]),
body: .init(data: data)
)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/wkhtmltopdf/Document+Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import NIO
extension Document {

public func generatePDF(on threadPool: NIOThreadPool = NIOThreadPool(numberOfThreads: 1), eventLoop: EventLoop) throws -> EventLoopFuture<Data> {
threadPool.start()
return threadPool.runIfActive(eventLoop: eventLoop) {
let fileManager = FileManager.default

Expand Down
2 changes: 1 addition & 1 deletion Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class wkhtmltopdfTests: XCTestCase {
let document = Document(margins: 15)
let page1 = Page("<p>Page from direct HTML</p>")
document.pages = [page1]
let data = try document.generatePDF(on: eventLoop).wait()
let data = try document.generatePDF(eventLoop: eventLoop).wait()
// Cop-out test, just ensuring that the returned data is something
XCTAssert(data.count > 50)
// Visual test
Expand Down