Skip to content

Commit

Permalink
Merge pull request #9 from grahamburgsma/vapor-4
Browse files Browse the repository at this point in the history
Upgrade to NIO 2
  • Loading branch information
jdmcd authored May 11, 2020
2 parents 48c520e + 909a4e0 commit bc8fc2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Packages
.build
.DS_Store
*.xcodeproj

Package.resolved
.swiftpm
16 changes: 9 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
// swift-tools-version:4.2
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "wkhtmltopdf",
products: [
.library(
name: "wkhtmltopdf",
targets: ["wkhtmltopdf"]),
targets: ["wkhtmltopdf"]
),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/service.git", from: "1.0.0")
.package(url: "https://github.com/apple/swift-nio.git", from: "2.13.1"),
],
targets: [
.target(
name: "wkhtmltopdf",
dependencies: [
"Service"
]),
.product(name: "NIO", package: "swift-nio")
]
),
.testTarget(
name: "wkhtmltopdfTests",
dependencies: ["wkhtmltopdf", "Vapor"]),
dependencies: ["wkhtmltopdf"]
),
]
)
10 changes: 5 additions & 5 deletions Sources/wkhtmltopdf/Document+Generate.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Foundation
import Service
import NIO

extension Document {

public func generatePDF(on container: Container) throws -> Future<Data> {
let sharedThreadPool = try container.make(BlockingIOThreadPool.self)

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

// Create the temp folder if it doesn't already exist
let workDir = "/tmp/vapor-wkhtmltopdf"
try fileManager.createDirectory(atPath: workDir, withIntermediateDirectories: true)

// Save input pages to temp files, and build up args to wkhtmltopdf
var wkArgs: [String] = [
"--zoom", self.zoom,
Expand Down
16 changes: 12 additions & 4 deletions Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import XCTest
import Vapor
import NIO
@testable import wkhtmltopdf

class wkhtmltopdfTests: XCTestCase {

var app: Application!
var group: EventLoopGroup!

override func setUp() {
super.setUp()

app = try! Application(config: .default(), environment: .testing, services: .default())
group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
}

override func tearDown() {
super.tearDown()

XCTAssertNoThrow(try group.syncShutdownGracefully())
}

func testStringPDF() throws {
let eventLoop = group.next()

let document = Document(margins: 15)
let page1 = Page("<p>Page from direct HTML</p>")
document.pages = [page1]
let data = try document.generatePDF(on: app).wait()
let data = try document.generatePDF(on: 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 bc8fc2a

Please sign in to comment.