菜鸟教程小白 发表于 2022-12-12 09:46:53

ios - 在 Xcode 中生成多页 PDF 文件


                                            <p><p>我正在尝试生成一个包含多个页面的 PDF 文件。我正在使用此代码:</p>

<pre><code>-(void)createPDFfromUI:(UIWebView *)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = ;

    // Points the pdf converter to the mutable data object and to the UIView to be converted

    UIWebView *myView = [ init];

    ;

    UIGraphicsBeginPDFContextToData(pdfData, myView.bounds, nil);

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    for (int i = 0; i &lt; 4; i++) {

      UIGraphicsBeginPDFPage();

      aView.layer.bounds = CGRectMake(0,480* -i , 320, 480);

      ;

    }

    ;

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = ;
    NSString* documentDirectoryFilename = ;

    // instructs the mutable data object to write its context to a file on disk
    ;
}
</code></pre>

<p>这段代码的问题是,它只生成一个包含 4 个相似页面的 PDF 文件,它们都是我原始页面的最后 480 像素。 (aView) 当我移动 `;在 FOR 循环之外,它会生成一个包含 4 页的 PDF 文件,但前 3 页将为空白,最后一页有一些数据。 </p>

<p>你们知道我该如何解决这个问题吗?! </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>bounds 表示层在其父层中的位置。所以当你渲染一个层时,它使用它的 <code>frame</code> 来确定它的内容。
您必须将 View 作为 subview 放置在另一个 View 中,例如 <code>aView2</code>。然后像你已经做的那样设置 <code>aView</code> 的边界,然后渲染 <code>aView2</code> 。</p>

<p>它应该可以工作。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Xcode 中生成多页 PDF 文件,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15499859/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15499859/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Xcode 中生成多页 PDF 文件