Forum


Replies: 7   Views: 443
Tcpdf_parser error: decodefilterflatedecode: invalid code
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by tarekas  · 03-07-2024 - 10:55

I am trying to merge pdf, then add text & image to pdf to sign, i am getting this error :

TCPDF_PARSER ERROR: decodeFilterFlateDecode: invalid code

the error is being thrown at this level of my code :
 

  $page_count = $pdf->setSourceFile($tmp_file);
            for ($page_no = 1; $page_no <= $page_count; $page_no++) {
                $pdf->AddPage();
                $template_id = $pdf->importPage($page_no);
                $pdf->useTemplate($template_id);
            }


Is this because the pdf is signed using third party tools?

How can i modify in this case? I need to sign it using TCPDF/TCPDI ..

Posted by admin  · 03-07-2024 - 19:02

Hello,

Please check the information available on Repair PDF documents. Please try repairing the PDF with gs as detailed on the previous documentation. Also please note that the PDF to be merged can't be protected (for example merging a signed document).

For further support, please send to contact[at]phpdocx.com the username or email of the user that purchased the license you are using, and a PDF sample. We'll check it.

Regards.

Posted by tarekas  · 11-07-2024 - 12:13

    public function merge_to_pdf($params)
    {
        $allowed_extensions = ['doc', 'docx', 'pdf'];
        $documents          = $params['documents'];
        $module             = $params['module'];

        $core_path = substr(COREPATH, 0, -12);
        require_once $core_path . '/application/libraries/phpdocx-premium-14-ns/Classes/Phpdocx/Create/CreateDocx.php';
        require_once $core_path . '/application/libraries/phpdocx-premium-14-ns/Classes/Phpdocx/Libs/TCPDF_lib.php';

        $module_attachments_folder_path = $this->get_module_container_path($module) . DIRECTORY_SEPARATOR;
        $module_tmp_folder_path         = $this->ci->config->item('files_path') . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
        $module_folder                  = $this->get_module_properties($module, 'container');
        if (is_dir($module_tmp_folder_path . $module_folder)) {
            $module_tmp_folder_path = $module_tmp_folder_path . $module_folder . DIRECTORY_SEPARATOR;
        }
        $pdf = new TCPDI();


I am trying to upgrade phpdocx to 14, I am encountering : undefined function TCPDI 
how should i call it?

Posted by admin  · 11-07-2024 - 12:24

Hello,

Thanks for sending the requested information about the license you are using.

Regarding your question, if you are using the namespaces package, you need to set the correct PHP namespace to instantiate a specific class. In the specific case of the TCPDI class, included in TCPDF_lib.php:

$pdf = new Phpdocx\Libs\TCPDI();

or using "use":

use Phpdocx\Libs\TCPDI;

$pdf = new TCPDI();

In any case, please note that phpdocx methods call TCPDF_lib internally. There's no need to generate a TCPDI class unless you need to use a feature not available as phpdocx method or option. For example, to merge PDF documents you can use mergePdf (https://www.phpdocx.com/api-documentation/docxutilities/merge-pdf-documents-with-PHP).

Regards.

Posted by tarekas  · 15-07-2024 - 08:29

 // require pdf libraries
        $core_path = substr(COREPATH, 0, -12);
        require_once $core_path . '/application/libraries/phpdocx-premium-14-ns/Classes/Phpdocx/Create/CreateDocx.php';
        require_once $core_path . '/application/libraries/phpdocx-premium-14-ns/Classes/Phpdocx/Libs/TCPDF_lib.php';

        // create the signed pdf
        $signed_pdf  = new Phpdocx\Libs\TCPDI();
        $page_count  = $signed_pdf->setSourceFile($pdf_to_sign);
        for ($page_no = 1; $page_no <= $page_count; $page_no++) {
            $signed_pdf->AddPage();
            $signed_pdf->SetAutoPageBreak(false);
            $template_id = $signed_pdf->importPage($page_no);
            $signed_pdf->useTemplate($template_id);
            $objects = array('signature' => $params['signature_picture'], 'stamp' => $params['stamp_picture'], 'name' => $params['user_full_name'], 'designation' => $params['designation'], 'date' => $params['sign_date']);
            foreach ($objects as $object => $object_value) {
                foreach ($params['user_signature_info']->$object->pages as $page_details) {
                    if ($page_no == $page_details->number) {
                        $template_size = $signed_pdf->getTemplateSize($template_id);
                        if(in_array($object, array('signature', 'stamp'))){
                            $signed_pdf->Image(file : $object_value, x : $page_details->coordinates->x, y : $page_details->coordinates->y, w : 60, h : $object == 'signature' ? 30 : 0);
                        }
                        else{
                            if(preg_match('/\p{Arabic}/u', $object_value)){
                                $signed_pdf->SetFont('aealarabiya', '', 14);
                            }
                            $signed_pdf->Text(x : $page_details->coordinates->x, y : $page_details->coordinates->y, txt : $object_value);
                        }
                    }      
                }
            }
        }


When changed to phpdocx 14, when signing the document, the pdf I'm getting is 1 blank page. Why?

Posted by admin  · 15-07-2024 - 15:17

Hello,

Please note that the available classes and methods to sign a PDF document are detailed on:

https://www.phpdocx.com/api-documentation/digital-signature-package/digitally-sign-docx-pdf-with-PHP

You need to use SignPDF or SignPDFPlus to sign a PDF document, not TCPDI with a custom code. Please note that TCPDI is an internal class used by phpdocx methods; it shouldn't be used standalone.

If you have any issue using SignPDF or SignPDFPlus to sign a PDF, please send this document to contact[at]phpdocx.com and we'll check it.

Please note we only can provide support for phpdocx methods. If you want to generate custom code with TCPDI we recommend you check the code available in phpdocx classes.

Regards.

Posted by tarekas  · 19-07-2024 - 11:05

I used signPDF with privatekeys and certificates, but gave same error : TCPDF_PARSER ERROR: decodeFilterFlateDecode.

Question: could it because the pdf is image-based pdf scanned by a scanner?

Is there any solution to this problem? this is critical. I don't need to use ghostscript, or any third parties.

Posted by admin  · 19-07-2024 - 17:28

Hello,

Maybe the PDF you are signing is protected (or not well-formed). Please send to contact[at]phpdocx.com a PDF sample and we'll check it.

As detailed on https://www.phpdocx.com/documentation/cookbook/repair-PDF, the PDF files must be well-formed and unprotected.

Regards.