1

現在のページを PDF として保存するように Safari に指示するために、スクリプト ブリッジを使用しようとしています。

「Safari.h」ヘッダー ファイルには、SafariItem クラスの保存メソッドが存在します。

- (void) saveAs:(NSString *)as in:(NSURL *)in_;

だから私はこれを使用しましたが、うまくいきません:

[safariCurrentTab saveAs:@".PDF" in:filePath];

あとでSafari.appの印刷オプションにPDF保存があることに気がついたので、この機能を使ってみました。

- (void) print:(NSURL *)x printDialog:(BOOL)printDialog withProperties:(SafariPrintSettings *)withProperties;

ただし、SafariPrintSettings オブジェクトを初期化しようとすると、コンパイル エラーが発生しました。

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_SafariPrintSettings", referenced from:
  objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

コンパイラはそのクラスを見つけられないようですが、ヘッダー ファイルをインクルードし、ScriptingBridge フレームワークを追加しました。

誰でも助けることができますか?

前もって感謝します。

4

1 に答える 1

2

So neither saveAs: nor SafariPrintSettings will work.

The "Save As..." functionality in the Safari app only provides HTML, and Scripting Bridge will only provide you with those options. That's the thing to keep in mind: Scripting Bridge is a way to automate things you would do in the interface.

Except for "printing to PDF". There is no printer object for producing PDFs. OS X calls upon one (or many, it's hard to say) utilities to convert the HTML to PDF.

Possible solution using NSTask

In /System/Libary/Printers/Libraries/ you'll find convert (which is not to be confused with the system-wide ImageMagick convert... they're not the same!). If you wanted to go this route, you'd have to save the page as an HTML file first, then you could use NSTask to run the convert command, like so:

NSTask *convert = [[NSTask alloc] init];
[convert setLaunchPath:@"/System/Libary/Printers/Libraries/convert"];
[convert setArguments:[NSArray arrayWithObjects:@"-f",@"fileYouSaved.html",@"-o","foo.pdf",nil]];
//set output and so on...
[convert launch];
//this runs the command as if it were "convert -f fileYouSaved.html -o foo.pdf"

But it seems flaky; some elements were left hanging off the "printable" page on some of the wider websites I tried.

Easiest possible solution

There is a third-party command line app/Ruby gem that might remove a lot of work called wkpdf. You would simply execute:

wkpdf --source http://www.apple.com --output apple.pdf

and it would grab the page online and do what you need. Alternatively, once you've installed it on your system, you can also call it in your app using NSTask, if you have more steps you need to take with the PDF file.

You can find wkpdf here: http://plessl.github.com/wkpdf/

于 2012-07-14T01:28:28.247 に答える