以相同的大小/比例导入JPEG和PDF

时间:2015-05-18 01:37:52

标签: php fpdf fpdi

我使用此脚本将PDF和JPEG都导入到单个PDF中。它运行良好并且可以正常工作,除了:图像不会以与PDF相同的比例/大小出现。

图像(所有横向)的范围从1280 x 960到4912 x 3264,我无法控制尺寸。该脚本为PDF顶部的单个图像执行AddPage()。

在最终输出中,JPEG的大小约为PDF的50%。如何使它们以相同的大小进行缩放,无论是屏幕输出还是文件输出?

$GLOBALS['txt'] = 'Cover Text';
$GLOBALS['mid_x'] = 11/2;

// iterate through the files
    $files = glob('pdfs/*.{pdf,PDF}', GLOB_BRACE);
    $imagefiles = glob('pdfs/*.{jpg,JPG,jpeg,JPEG}', GLOB_BRACE);   

require('fpdf17/fpdf.php');
require('fpdi/fpdi.php');

$pdf = new FPDI("L","in","Letter");
$pdf->SetMargins(.1,.1,.1);
$pdf->AddPage();
$pdf->Image('newimages/03-March-2013.jpg', 0, 0, 11, 8.5, "JPG");
$pdf->SetFont('Arial','',48);
$pdf->SetTextColor(255,255,255); // White
// Centering a cell with unknown text length
$pdf->Text($GLOBALS['mid_x']-$pdf->GetStringWidth($GLOBALS['txt'])/2,7,$GLOBALS['txt']);
$im = 0;
$pageNo = 0;
foreach ($files AS $filename) {
// get the page count
$pageCount = $pdf->setSourceFile($filename);
// iterate through all pages
$pdf->AddPage('L', array("11", "8.5"));
$pdf->Image('newimages/' . basename($imagefiles[$im]), 0, 0, 11, 8.5, "JPG");
$pdf->Text(1.5,7,basename($imagefiles[$im]));
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);
    // get the size of the imported page
    $size = $pdf->getTemplateSize($templateId);
    // create a page (landscape or portrait depending on the imported page size)
    if ($size['w'] > $size['h']) {
        $pdf->AddPage('L', array($size['w'], $size['h']));
    } else {
        //$pdf->AddPage('P', array($size['w'], $size['h']));
    }
    // use the imported page
    $pdf->useTemplate($templateId); 
    }
    $im = ($im + 1);
    }
// Output the new PDF
$pdf->Output();

0 个答案:

没有答案