如何通过FPDI将外部pdf文件作为附加页面添加到TCPF

问题描述

我正在使用TCPDF从php文件创建PDF文件。一切正常。现在,我想使用服务器上现有的.pdf文件添加其他页面。

最好的方法是使用FPDI afaik。

但是我找不到任何有关如何在TCPDF中设置FPDI来添加页面的文档或工作示例。我所看到的就是我如何使用外部pdf作为标题或背景等。

喜欢这个 https://www.setasign.com/products/fpdi/about/

我在TCPDF中拥有什么

use setasign\Fpdi\Tcpdf\Fpdi;

// require_once('tcpdf/config/lang/eng.php');
require_once('TCPDF-main/tcpdf.php');
require_once('FPDI/src/autoload.php');

// Extend the TCPDF class to create custom Footer
class MYPDF extends TCPDF {

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(),false,'C','','T','M');
    }
}

// add external PDF with FPDI (not working)

$pdf = new MYPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',true);

$pageCount = $pdf->setSourceFile("hiking.pdf");
$tplIdx = $pdf->importPage(1,'/flyer');

$pdf->addPage();
$pdf->useTemplate($tplIdx,90);

$pdf->Output();


// create new PDF document with TCPDF (working)

$pdf = new MYPDF(PDF_PAGE_ORIENTATION,true);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(true);

$pdf->AddPage();
ob_start(); //Start new output buffer

//Write page 1
$html = include('mytour.php');

$html = ob_get_contents();

$pdf->writeHTML($html,0);

// reset pointer to the last page
$pdf->lastPage();
ob_end_clean();

// ---------------------------------------------------------

$cdate = date("y-d-m");
$ctime = date("H-i-s");

//Close and output PDF document
$output1 = "mytour.pdf";
$pdf->Output($output1,'I');

我收到的错误: PHP致命错误:未捕获错误:调用未定义方法MYPDF :: setSourceFile()

赞赏有关操作方法的任何提示。

解决方法

只需按照here中所述扩展正确的类:

import pandas as pd

def weekinmonth(dates):
    """Get week number in a month.
    
    Parameters: 
        dates (pd.Series): Series of dates.
    Returns: 
        pd.Series: Week number in a month.
    """
    firstday_in_month = dates - pd.to_timedelta(dates.dt.day - 1,unit='d')
    return (dates.dt.day-1 + firstday_in_month.dt.weekday) // 7 + 1
    

df = pd.DataFrame(pd.date_range(' 1/ 1/ 2000',periods = 100,freq ='D'),columns=['Date'])
weekinmonth(df['Date'])

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...