PHPで既存のPDFに上書きする方法

Pocket

PHPでPDFを読み込み、上書きする方法
こちらを参照し、テスト:
http://stackoverflow.com/questions/1418945/tcpdf-start-with-existing-pdf-document
必要なのは、

両方をダウンロードし、実行環境に入れる。
サンプルコード

require_once( 'assets/tcpdf/config/my_tcpdf_config.php'); // 諸々DEFINE
require_once( 'assets/tcpdf/tcpdf.php');
require_once( 'assets/tcpdf/fpdi/fpdi.php');
class PDF extends FPDI {
    var $_tplIdx;
    function Header(){
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile( PATH_IMAGES . 'hoge.pdf');
            $this->_tplIdx = $this->importPage(1);
        }
        $size = $this->useTemplate($this->_tplIdx);
    }
    function Footer(){
    }
}
// initiate PDF 横書き
$pdf = new PDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);
// add a page
$pdf->AddPage();
// get external file content
$utf8text = file_get_contents('assets/example.txt', true);
$pdf->SetFont('freeserif', '', 12);
// now write some text above the imported page
$pdf->Write(5, $utf8text);
$pdf->Output();

もし実行してエラーログに

PHP Fatal error: Uncaught exception 'Exception' with message 'File is encrypted!

と出る場合は、pdfがパスワードプロテクトされていないか等を確認する。Encrpytされているかは、pdfを右クリック→Get Infoでわかる:
encrypted