brentnycum / BNHtmlPdfKit

Easily turn HTML data from an HTML string or URL into a PDF file on iOS
MIT License
270 stars 62 forks source link

saveWebViewAsPdf doesn't generate the PDF #19

Open Isuru-Nanayakkara opened 9 years ago

Isuru-Nanayakkara commented 9 years ago

I tried this first in a Swift project. I added the library via CocoaPods.

platform :ios, '8.0'
use_frameworks!

pod 'BNHtmlPdfKit', :git => 'https://github.com/brentnycum/BNHtmlPdfKit'

Here's the code.

var htmlPdfKit: BNHtmlPdfKit!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com/")!))
}

@IBAction func saveButtonTapped(sender: UIBarButtonItem) {
    let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
    let documentsDirectoryPathURL = NSURL(string: documentsDirectoryPath)!
    let pdfPath = documentsDirectoryPathURL.URLByAppendingPathComponent("Google.pdf")
    print(pdfPath.absoluteString)

    htmlPdfKit = BNHtmlPdfKit()
    htmlPdfKit.delegate = self
    htmlPdfKit.pageSize = BNPageSizeA4
    htmlPdfKit.saveWebViewAsPdf(webView, toFile: pdfPath.absoluteString)
}

When the save button is tapped, the PDF isn't generated. Nor any of the BNHtmlPdfKitDelegate methods get called.

However other methods like saveUrlAsPdf and saveHtmlAsPdf do work. I tested this in Xcode 7.1 with iOS 8.4 and iOS 9.1 simulators.

I've uploaded a demo project too.

Later I tested this in a Objective-C project as well. And it didn't work either. So I guess this isn't a language issue.

vin-the-dev commented 8 years ago

@Isuru-Nanayakkara did you get to fix it?

Isuru-Nanayakkara commented 8 years ago

@vinbhai4u Nope, I had to resort to saving via a HTML string.

vin-the-dev commented 8 years ago

@Isuru-Nanayakkara even that doesn't work for me,

generateBNHtmlPDF(){ let pdfKit = BNHtmlPdfKit() pdfKit.delegate = self let toFile = Path.documentsDir["file.pdf"].path_string let htmlString = Path.documentsDir["BSM.html"].readString() pdfKit.saveHtmlAsPdf(htmlString, toFile: toFile) print("open " + toFile) }

is there something wrong?

shutup commented 8 years ago

i also can not generate pdf from an webView, follow the code, i add the save method in the - (void)saveWebViewAsPdf:(UIWebView )webView toFile:(NSString )file; And it work now. i am not sure if this is correct ,but the doc has no use case on this .

-(void)saveWebViewAsPdf:(UIWebView )webView toFile:(NSString )file { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(_timeout) object:nil]; self.outputFile = file; webView.delegate = self; self.webView = webView; //add for test [self _savePdf]; }

mwaqasbhati commented 7 years ago

Hey, I have solved the issue while generating pdf file from webview --saveWebViewAsPdf(webView, toFile: pdfPath.absoluteString) and now delegates are being called properly with the path of saved pdf file.

Actually I have done my changes after forking the project. Infact I have make a test project for this but unfortunately I don't know how to push my changes.