randomstate/camelot-php

PHP 封装库,用于与用 Python 构建的 Camelot PDF 表格提取库进行接口交互

v1.0.1 2019-12-12 21:13 UTC

This package is auto-updated.

Last update: 2024-09-13 06:52:42 UTC


README

Camelot 的 PHP 封装,Camelot 是一个 Python PDF 表格提取库

安装

composer require randomstate/camelot-php

使用

该包紧密遵循 camelot CLI API 使用。默认输出为 CSV 格式的简单字符串。如果您需要解析 CSV 字符串,我们建议使用 league/csv 包(https://csv.thephpleague.com/

<?php

use RandomState\Camelot\Camelot;
use League\Csv\Reader;

$tables = Camelot::lattice('/path/to/my/file.pdf')
       ->extract();

$csv = Reader::createFromString($tables[0]);
$allRecords = $csv->getRecords();

高级处理

保存/提取

注意:在运行以下方法之一之前,不会执行任何 Camelot 操作

$camelot->extract(); // uses temporary files and automatically grabs the table contents for you from each
$camelot->save('/path/to/my-file.csv'); // mirrors the behaviour of Camelot and saves files in the format /path/to/my-file-page-*-table-*.csv
$camelot->plot(); // useful for debugging, it will plot it in a separate window (see Visual Debugging below)   
设置格式
$camelot->json();
$camelot->csv();
$camelot->html();
$camelot->excel();
$camelot->sqlite();
指定页面编号

$camelot->pages('1,2,3-4,8-end')

读取加密的 PDF 文件

$camelot->password('my-pass')

处理背景行

$camelot->stream()->processBackgroundLines()

视觉调试

$camelot->plot()

指定表格区域
<?php

use RandomState\Camelot\Camelot;
use RandomState\Camelot\Areas;

Camelot::stream('my-file.pdf')
    ->inAreas(
        Areas::from($xTopLeft, $yTopLeft, $xBottomRight, $yBottomRight)
            // ->add($xTopLeft2, $yTopLeft2, $xBottomRight2, $yBottomRight2)
            // ->add($xTopLeft3, $yTopLeft3, $xBottomRight3, $yBottomRight3)
    );
指定表格区域
<?php

use RandomState\Camelot\Camelot;
use RandomState\Camelot\Areas;

Camelot::stream('my-file.pdf')
    ->inRegions(
        Areas::from($xTopLeft, $yTopLeft, $xBottomRight, $yBottomRight)
            // ->add($xTopLeft2, $yTopLeft2, $xBottomRight2, $yBottomRight2)
            // ->add($xTopLeft3, $yTopLeft3, $xBottomRight3, $yBottomRight3)
    );
指定列分隔符

$camelot->stream()->setColumnSeparators($x1,$x2...)

沿分隔符分割文本

$camelot->split()

标记上标和下标

$camelot->flagSize()

从文本中删除字符

$camelot->strip("\n")

改进猜测的表格区域

$camelot->setEdgeTolerance(500)

改进猜测的表格行

$camelot->setRowTolerance(15)

检测短行

$camelot->lineScale(20)

调整跨单元格中的文本

$camelot->shiftText('r', 'b')

复制跨单元格中的文本

$camelot->copyTextSpanningCells('r', 'b')

许可协议

MIT. 自行承担风险,我们不承担此代码使用方式的任何责任。