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

saveWebViewAsPdf doesn't generate the PDF #19

Open
Isuru-Nanayakkara opened this issue Nov 14, 2015 · 5 comments
Open

saveWebViewAsPdf doesn't generate the PDF #19

Isuru-Nanayakkara opened this issue Nov 14, 2015 · 5 comments

Comments

@Isuru-Nanayakkara
Copy link

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
Copy link

@Isuru-Nanayakkara did you get to fix it?

@Isuru-Nanayakkara
Copy link
Author

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

@vin-the-dev
Copy link

@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
Copy link

shutup commented Mar 11, 2016

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants