doureng1083 2013-03-05 23:10
浏览 156
已采纳

FPDF - 它可以处理具有混合方向(纵向/横向)的原始PDF吗?

I am using the FPDF() method (from FPDI_Protection.php) to import existing PDFs and apply password protection.

The problem I'm having is that the original PDF has a mix of portrait and landscape pages (8.5"X11" & 11"X8.5"), whereas the import method makes you define it once. I can define the newly created pdf to be 11"X11", which fixes the problem of one of the orientations cropping, but this is not ideal for printing purposes, as the PDF is scaled and left aligned, causing poor readability/printout.

Is there any sort of routine I could use, as the original document is being looped through, to detect the original size and set the new page orientation on the fly?

function pdfEncrypt ($origFile, $password, $destFile)  // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES
{
    require_once('fpdi/FPDI_Protection.php');

    $pdf = new FPDI_Protection();
    // set the format of the destinaton file, in our case 6×9 inch
    $pdf->FPDF('P', 'in', array('11','11'));

    //calculate the number of pages from the original document
    $pagecount = $pdf->setSourceFile($origFile);

    // copy all pages from the old unprotected pdf in the new one
    for ($loop = 1; $loop <= $pagecount; $loop++)
    {
        $tplidx = $pdf->importPage($loop);
        $pdf->addPage();
        $pdf->useTemplate($tplidx);
    }

    // protect the new pdf file, and allow no printing, copy etc and leave only reading allowed
    $pdf->SetProtection(array('print'), $password, '');
    $pdf->Output($destFile, 'F');

    return $destFile;
}

Or, alternatively, is there a simpler way to add a password to an existing pdf using php?

  • 写回答

3条回答 默认 最新

  • dpjr86761 2013-03-06 23:21
    关注

    Ok, I pulled my hair out for days on this one. After tireless googling every iteration of terms related to my problem I was able to find one instance of a solution that actually worked (I tried installing pdflib lite, phpinfo, ghostscript, xpdf, etc. etc., to measure dimensions to no avail). What worked was this (you need the FPDI_Protection package [free]):

        $specs = $pdf->getTemplateSize($tplidx);
        $pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
    

    The full function is as follows:

    function pdfEncrypt ($origFile, $password, $destFile)  // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES
    {
        require_once('fpdi/FPDI_Protection.php');
    
        $pdf = new FPDI_Protection();
        // set the format of the destinaton file
        $pdf->FPDF('P', 'in', array('8.5','11'));
    
        //calculate the number of pages from the original document
        $pagecount = $pdf->setSourceFile($origFile);
    
        // copy all pages from the old unprotected pdf in the new one
        for ($loop = 1; $loop <= $pagecount; $loop++)
        {
    
            $tplidx = $pdf->importPage($loop);
    
            $specs = $pdf->getTemplateSize($tplidx);
            $pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
            $pdf->useTemplate($tplidx);
        }
    
        // protect the new pdf file
    
        $pdf->SetProtection(array('print'), $password, '');
        $pdf->Output($destFile, 'F');
    
        return $destFile;
    }
    

    The addition of those two lines of code was able to detect whether the original page was portrait of landscape, and recreate the page in the output file the same way. Hallelujah.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题