0

現在、AirPrint を使用して iPad から PDF を印刷しようとしています。PDFをA4サイズ縦向きで直接印刷すると大丈夫でした。

しかし、今は PDF を A5 サイズで縦向きに印刷したいのですが、現在は作成できない A4 用紙の半分のサイズのようです。PDFファイルのサイズを変更する必要があるか(望ましくない)、他の方法でそれを行う必要があります。

私は使用してUIPrintInteractionControllerおり、印刷するデリゲートですが、ソースはviewPrintFormatterPDFをロードするwebViewからのものです。

PageRenderer:

    let fmt = webView.viewPrintFormatter()

    // 2. Assign print formatter to UIPrintPageRenderer

    let render = UIPrintPageRenderer()
    render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)

    // 3. Assign paperRect and printableRect

    let page = CGRect(x: 0, y: 0, width:595.2, height: 841.8) // A4, 72 dpi
    let printable = CGRectInset(page, 0, 0)

    render.setValue(NSValue(CGRect: page), forKey: "paperRect")
    render.setValue(NSValue(CGRect: printable), forKey: "printableRect")

    // 4. Create PDF context and draw

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)

    for i in 1...render.numberOfPages() {

        UIGraphicsBeginPDFPage();
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPageAtIndex(i - 1, inRect: bounds)
    }

    UIGraphicsEndPDFContext();

次に、物理ファイルとして保存します。

    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

    let url = NSURL(fileURLWithPath: "\(documentsPath)/file.pdf")
    pdfData.writeToFile("\(documentsPath)/file.pdf", atomically: true)

そして最後にそれを印刷します:

        if UIPrintInteractionController.canPrintURL(url) {
            let printInfo = UIPrintInfo(dictionary: nil)
            printInfo.jobName = url.lastPathComponent!
            printInfo.outputType = .Grayscale
            printInfo.orientation = .Landscape

            let printController = UIPrintInteractionController.sharedPrintController()
            printController.printInfo = printInfo
            printController.showsPaperSelectionForLoadedPapers = true

            printController.showsNumberOfCopies = true
            printController.delegate = self
            printController.printingItem = url

            printController.presentAnimated(true, completionHandler: nil)
        }
4

1 に答える 1