PDFを作る/印刷する

全体の流れ
1.PDFデータを格納する変数またはファイルを用意する
2.ページのスタート
3.内容を書く
4.プリンタに送る

※今回はPDFをデータにしたけど、ファイルにして、それをUIDocumentInteractionControllerに送っても良い。

//PDFデータを格納する変数またはファイルを用意する(今回はデータ)
NSMutableData*pdfData=[[NSMutableData alloc]initWithCapacity:0];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

//1ページ目スタート(A4サイズ)
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0,612,792),nil);

//文字列書き込み
[@”書き込む文字列” drawInRect:CGRectMake(0,0,100,30)];
//イメージ書き込み
[[UIImage imageNamed:@”書き込むイメージファイル”] drawInRect:CGRectMake(0,0,100,100)];

//書き込み終了
UIGraphicsEndPDFContext();

//プリンタに送る
UIPrintInfo*printInfo=[UIPrintInfo printInfo];
printInfo.jobName=@”printjob”;
printInfo.orientation=UIPrintInfoOrientationPortrait;
printInfo.duplex=UIPrintInfoDuplexLongEdge;
printInfo.outputType=UIPrintInfoOutputGeneral;
UIPrintInteractionController*pCon=[UIPrintInteractionController sharedPrintController];
pCon.printInfo=printInfo;
pCon.printingItem=pdfData;
pCon.showsPageRange=NO;
pCon.delegate=self;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printDocController, BOOL completed, NSError *error) {
        NSLog(@”%d”,completed);
        if (!completed && error) {
            NSLog(@”PrintError %@”, error);
        }
};
[pCon presentAnimated:YES completionHandler:completionHandler];

//UIPrintInteractionControllerのdelegate
– (UIViewController *)printInteractionControllerParentViewController:(UIPrintInteractionController *)printInteractionController{
    //UIPrintInteractionControllerの親になる(下になる)ViewControllerを返す
    return self;
}