Andreas Prang
Andreas Prang

Reputation: 2217

WebView to PDFDocument

I tried to get a PDFDocument from a WebView but I only got a part of the Page :-(

The code I used is this:

//Generate a PDF document from the HTML.
NSData *pdfData = [webView dataWithPDFInsideRect:[webView frame]];
PDFDocument *document = [[PDFDocument alloc] initWithData:pdfData];

[document writeToURL:[NSURL URLWithString:@"file:///Users/tmp/test.pdf"]];

Any idea how to make it better?

Upvotes: 2

Views: 1512

Answers (2)

Ramaraj T
Ramaraj T

Reputation: 5230

NSData *pdfData = [[[[webview mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webview mainFrame] frameView] documentView].frame]; 
PDFDocument *document = [[PDFDocument alloc] initWithData:pdfData];
[document writeToFile:@"/Users/PalmAgent/Desktop/test.pdf"];

Upvotes: 4

Peter Hosey
Peter Hosey

Reputation: 96323

  1. Get the web view's main frame.
  2. Get the main frame's frame view.
  3. Get the main frame view's document view.
  4. Get the PDF data from that, passing the document view's bounds (not frame) for the rect.

Upvotes: 3

Related Questions