Skip to content

Commit

Permalink
Merge pull request #10 from grahamburgsma/fix-threadpool
Browse files Browse the repository at this point in the history
Fix threadpool usage and update README
  • Loading branch information
jdmcd authored May 12, 2020
2 parents bc8fc2a + 77d66d3 commit 48b4819
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
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

0 comments on commit 48b4819

Please sign in to comment.