kayukoff / camelot-php
PHP 客户端库,用于与 Python 中构建的 Camelot PDF 表格提取库进行接口交互
Requires
- php: >=8.1
- spatie/temporary-directory: ^2.0
- symfony/process: ^5.0|^6.0
Requires (Dev)
- league/csv: ^9.8
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-30 02:00:10 UTC
README
Camelot 的 PHP 客户端,用于 Python PDF 表格提取库。
从 randomstate/camelot-php 分支更新,以支持 Symfony 6 和 PHP 8。
安装
composer require kayukoff/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。自行承担风险,我们不承担使用此代码的任何责任。