开发者

Printing from an iOS device (AirPrint)

I think I must be coming at this from the wrong angle. This is my first foray into PDF with Quartz. I would like to have the ability to print text that is entered into my app, but I may need to drop it in this release.

I have read everything I can get my hands on both from Apple and the Web, but I can't seem to get my stuff working. I can get the document to the printer but it always prints blank. The process I am using is:

  1. Get the text as NSString.
  2. Convert the string to a PDF in a NSData object.
  3. Print it.

I have two methods in my text project. This is only a test so there are no bells and whistles and there are probably memory leaks, I'm just trying to get it to work.

Thanks in advance for any help.

The first creates the PDF document as NSData.

- (NSData *)createPdfAsDataWithAttributedString:(NSAttributedString *)text {
    // Save the text just in case…
    _pdfText = [text copy];

    // Allocate the pdf Context.
    CGContextRef pdfContext;

    // Create the PDF Attribute Dictionary.
    CFMutableDictionaryRef pdfAttributeDictionary = NULL;
    pdfAttributeDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(pdfAttributeDictionary, kCGPDFContextTitle, CFSTR("My Note"));
    CFDictionarySetValue(pdfAttributeDictionary, kCGPDFContextCreator, CFSTR("Me"));

    // Create the data referece as a mutable data type.
    NSData *pdfData = [[NSMutableData alloc] init];

    // User the data consumer using the data reference.
    CGDataConsumerRef pdfDataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData);

    // Finally the pdfContext can be created.
    pdfContext = CGPDFContextCreate(pdfDataConsumer, NULL, pdfAttributeDictionary);

    // Start the first page.
    CGContextBeginPage(pdfContext, NULL);

    // Set the font
    CGContextSelectFont(pdfContext, "Helvetica", 16.0f, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
    CGContextSetRGBFillColor(pdfContext, 0, 0, 0, 1);

    // Print the text
    NSString *regText = [text string];
    const char *pdfText = [regText cStringUsingEncoding:NSUTF8StringEncoding];
    CGContextShowText(pdfContext, pdfText, strlen(pdfText));

    // End the page.
    CGContextEndPage(pdfContext);

    // Save the current state
    CGContextSaveGState(pdfContext);    

    // Release the PDF context
    CGContextRelease(pdfContext);

    return [pdfData autorelease];
}

The Second is the print.

- (IBAction)printPdfTouchUpInside:(id)sender {
    PDFCreator *pdfCreator = [[PDFCreator alloc] init];
    NSData *pdfData = [pdfCreator createPdfAsDataWithAttributedString:_printableText];
    UIPrintInteractionController *printController = 开发者_如何转开发[UIPrintInteractionController sharedPrintController];
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    [printInfo setPrinterID:@"Canon MP620 series"];
    [printInfo setOrientation:UIPrintInfoOrientationPortrait];
    [printInfo setOutputType:UIPrintInfoOutputGeneral];
    [printInfo setJobName:@"Test Print"];

    [printController setPrintInfo:printInfo];
    [printController setPrintingItem:pdfData];

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"FAILED!  due to error in domain %@ with error code %u", error.domain, error.code);
        }
    };

    [printController presentFromRect:[sender bounds] inView:sender animated:YES completionHandler:completionHandler];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜