Codeigniter 中的 PdfParser

PdfParser in Codeigniter

我正在尝试在 Codeigniter 中使用 PdfParser 库 ( PDFparser link ),所以首先我已经下载、解压缩并安装了 third_party/vendor 文件夹下的库,另外我还下载了,解压缩并安装 tcpdf 库在同一 third_party/vendor 文件夹中。 由于我无法在远程服务器上使用 运行 Composer,因此我在 thir_party/vendor 文件夹中创建了一个 autoload.php 文件。 文件夹结构现在如下:

third_party
   vendor
      autoload.php
      tecnickcom
         tcpdf
      smalot
         pdfparser
            src
               Smalot
                  PdfParser

自动加载文件中加载所有依赖项的代码如下:

    $vendorDir = '../vendor';
$tcpdf_files = Array(
    'Datamatrix' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/datamatrix.php',
    'PDF417' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/pdf417.php',
    'QRcode' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/qrcode.php',
    'TCPDF' => $vendorDir . '/tecnickcom/tcpdf/tcpdf.php',
    'TCPDF2DBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_2d.php',
    'TCPDFBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_1d.php',
    'TCPDF_COLORS' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_colors.php',
    'TCPDF_FILTERS' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_filters.php',
    'TCPDF_FONTS' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_fonts.php',
    'TCPDF_FONT_DATA' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_font_data.php',
    'TCPDF_IMAGES' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_images.php',
    'TCPDF_IMPORT' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_import.php',
    'TCPDF_PARSER' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_parser.php',
    'TCPDF_STATIC' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_static.php'
);

foreach ($tcpdf_files as $key => $file) {
    include_once $file;
}

include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Parser.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Document.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Header.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Object.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Page.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Pages.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementArray.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementBoolean.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementString.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementDate.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementHexa.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementMissing.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementName.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementNull.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementNumeric.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementStruct.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Element/ElementXRef.php";

include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding/StandardEncoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding/ISOLatin1Encoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding/ISOLatin9Encoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding/MacRomanEncoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Encoding/WinAnsiEncoding.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font/FontCIDFontType0.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font/FontCIDFontType2.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font/FontTrueType.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font/FontType0.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/Font/FontType1.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/XObject/Form.php";
include_once  $vendorDir . "/smalot/pdfparser/src/Smalot/PdfParser/XObject/Image.php";

我在 Codeigniter 库文件夹中创建了一个新文件,文件名为 Pdfparser.php,其内容如下:

    defined('BASEPATH') OR exit('No direct script access allowed');

class Pdfparser {
    function __construct() {
        require_once APPPATH.'/third_party/vendor/autoload.php';
    }
}

在控制器中,我手动加载 libray 文件:

$this->load->library('pdfparser');

问题是当我尝试用

实例化 class 时
$parser = new \Smalot\PdfParser\Parser();

我收到以下错误:

PHP Fatal error:  Class 'Smalot\PdfParser\Parser' not found

有什么解决这个问题的建议吗?非常感谢

我猜你使用版本控制?

在本地通过 composer 安装库(我看到你刚刚下载并解压缩 - 这不起作用)。只需使用 vendor.

的正常目录结构即可

您的项目根目录中应该有 composer.jsoncomposer.lock。安装后,composer 还会创建类映射文件等,它需要这些文件才能找到您的 类.

所以。您可以将 vendor 文件夹放入 Git(这可能会变得有点混乱,具体取决于您 运行 composer 命令的频率),或者您可以只使用像 scp 这样的工具来安全地复制和将供应商文件部署到位。

希望对您有所帮助!