2017-06-13 3 views
0

3私が持っている私は、PDFに描画しようとしている単一ページのPDFはドローFUNCとかなり簡単だった作ってるんだものNSAttributedStringかなり長い:NSAttributedStringからマルチPDFを生成し、迅速

func createPDFFilea(atext: NSAttributedString) -> NSMutableData { 

    let pdfData = NSMutableData() 
    let paperRect = CGRect(x: 0, y: 0, width: 595.2, height: 841.8); 
    UIGraphicsBeginPDFContextToData(pdfData, paperRect, nil) 
    UIGraphicsBeginPDFPage() 

    atext.draw(in: paperRect) 
    UIGraphicsEndPDFContext() 

    return pdfData 
} 

が、テキストの場合paperRectを超えると、これは失われます、これをどのように管理するのですか?

PS。ここでOBJ-Cのための簡単なNSString http://www.coderzheaven.com/2016/09/07/create-pdf-in-ios/ と同様のソリューションが、私はスウィフトにそれを得るために把握し、また、Appleの公式ドキュメントを思わすることができない私は、変換するために、最終的に管理しているOBJ-C https://developer.apple.com/library/content/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html

+0

をswift3するOBJ-Cのコード私は最近のものと同じ種類のやって持っていた - 私は印刷するマルチセクションテーブルを持っている、複数のページにわたって分割することができます。私の解決策は、一度に1行をUIWebViewに追加し、 'webViewDidFinishLoad'を使用してページサイズを確認することでした。以前のhtmlテキストを使用したページを超え、新しいUIWebViewを作成しました。すべてのWebページが完成したら、複数ページのPDFを作成するのは簡単な作業です – Russell

答えて

0

のみです

func createPDFwithAttributedString(_ currentText: NSAttributedString) -> NSMutableData { 

    let pdfData = NSMutableData() 

    // Create the PDF context using the default page size of 612 x 792. 
    UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil) 

    let framesetter = CTFramesetterCreateWithAttributedString(currentText) 

    var currentRange = CFRangeMake(0, 0); 
    var currentPage = 0; 
    var done = false; 

    repeat { 
     // Mark the beginning of a new page. 
     UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: 612, height: 792), nil); 

     // Draw a page number at the bottom of each page. 
     currentPage += 1; 

     // Render the current page and update the current range to 
     // point to the beginning of the next page. 
     renderPagewithTextRange(currentRange: &currentRange, framesetter: framesetter) 

     // If we're at the end of the text, exit the loop. 
     if (currentRange.location == CFAttributedStringGetLength(currentText)){ 
      done = true; 
     } 
    } while (!done); 

    // Close the PDF context and write the contents out. 
    UIGraphicsEndPDFContext(); 
    return pdfData 
} 

func renderPagewithTextRange (currentRange: inout CFRange, framesetter: CTFramesetter) { 
    // Get the graphics context. 
    if let currentContext = UIGraphicsGetCurrentContext(){ 

     // Put the text matrix into a known state. This ensures 
     // that no old scaling factors are left in place. 
     currentContext.textMatrix = CGAffineTransform.identity; 

     // Create a path object to enclose the text. Use 72 point 
     // margins all around the text. 
     let frameRect = CGRect(x: 72, y: 72, width: 468, height: 648); 
     let framePath = CGMutablePath(); 
     framePath.addRect(frameRect) 

     // Get the frame that will do the rendering. 
     // The currentRange variable specifies only the starting point. The framesetter 
     // lays out as much text as will fit into the frame. 
     let frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, nil); 

     // Core Text draws from the bottom-left corner up, so flip 
     // the current transform prior to drawing. 
     currentContext.translateBy(x: 0, y: 792); 
     currentContext.scaleBy(x: 1.0, y: -1.0); 

     // Draw the frame. 
     CTFrameDraw(frameRef, currentContext); 

     // Update the current range based on what was drawn. 
     currentRange = CTFrameGetVisibleStringRange(frameRef); 
     currentRange.location += currentRange.length; 
     currentRange.length = 0; 
    } 
}