Skip to content

Commit

Permalink
Fix images not appearing in PDF export
Browse files Browse the repository at this point in the history
  • Loading branch information
lukakerr committed May 23, 2019
1 parent 45b3edd commit cb62655
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Pine/Controllers/MarkdownViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Cocoa
import WebKit
import Highlightr
import cmark_gfm_swift

Expand All @@ -17,6 +18,14 @@ class MarkdownViewController: NSViewController, NSTextViewDelegate, HighlightDel
public var markdownTextView: MarkdownTextView!
public var textStorage: CodeAttributedString!

public var previewWebView: WKWebView? {
return self.previewViewController?.webPreview
}

public var document: Document? {
return self.windowController?.document as? Document
}

private var scrollView: NSScrollView!
private var layoutManager: MarkdownLayoutManager!
private var debouncedGeneratePreview: Debouncer!
Expand Down
2 changes: 1 addition & 1 deletion Pine/Controllers/PineWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ extension PineWindowController {
// MARK: - First responder methods for exporting

@IBAction func exportPDF(sender: NSMenuItem) {
PDFExporter.export(from: previewViewController?.webPreview)
PDFExporter.export(from: markdownViewController)
}

@IBAction func exportHTML(sender: NSMenuItem) {
Expand Down
13 changes: 9 additions & 4 deletions Pine/Helpers/Exporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ struct PDFExporter: Exportable {
static var filetype = "pdf"

static func export<T>(from view: T) {
// We expect the view to be a webview from which we can HTML and print from
let webview = view as! WKWebView
// We expect the view to be a markdown view where we can get the document and HTML
let markdownViewController = view as! MarkdownViewController

webview.evaluateJavaScript("document.documentElement.outerHTML") { (response, err) in
let doc = markdownViewController.document
let docURL = doc?.fileURL ?? URL(fileURLWithPath: "/")

let webview = markdownViewController.previewWebView

webview?.evaluateJavaScript("document.documentElement.outerHTML") { (response, err) in
if let HTMLString = response as? String {
let HTMLData = "<!DOCTYPE HTML>" + HTMLString
let wv = WebView()
wv.mainFrame.loadHTMLString(HTMLData, baseURL: nil)
wv.mainFrame.loadHTMLString(HTMLData, baseURL: docURL)

let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when) {
Expand Down

0 comments on commit cb62655

Please sign in to comment.