PNG JPG BMP TIFF PDF
Aspose.PDF  for PHP

在PHP SDK中添加表格到PDF

使用云PHP SDK向PDF文档添加表格

Get Started

如何通过云PHP SDK向PDF添加表格

为了将表格添加到PDF中,我们将使用 Aspose.PDF Cloud PHP SDK 这个云SDK帮助PHP程序员使用Aspose.PDF REST API开发基于云的PDF创建、注释、编辑和转换应用程序。只需在Aspose for Cloud创建一个帐户并获取您的应用程序信息。一旦您拥有了App SID和密钥,您就可以开始使用Aspose.PDF Cloud PHP SDK。

包管理器控制台命令

     
    composer install

通过云PHP添加表格到PDF的步骤

Aspose.PDF Cloud开发人员可以轻松地在几行代码中加载并添加表格到PDF。

  1. 上传PDF文档
  2. 下载处理后的PDF
  3. 初始化一个表格对象
  4. 用数据填充表格
  5. 将表格添加到PDF页面
  6. 执行工作流程
 

使用PHP添加表格到PDF


    private function _create_rest_api() {
            $credentials = json_decode(file_get_contents("../../../../Credentials/credentials.json"), true);

            $configAuth = new Configuration();
            $configAuth->setAppKey($credentials['key']);
            $configAuth->setAppSid($credentials['id']);

            $this->pdfApi = new PdfApi(null, $configAuth);
        }

        public function __construct($config) {
            $this->configParams = $config;
            $this->_create_rest_api();
        }

        public function uploadDocument() {
            $fileNamePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['PDF_DOCUMENT_NAME'];
            $pdfFileData = file_get_contents($fileNamePath);
            $this->pdfApi->uploadFile($this->configParams['PDF_DOCUMENT_NAME'], $pdfFileData);
        }

        public function downloadResult() {
            $changedPdfData = $this->pdfApi->downloadFile($this->configParams['PDF_DOCUMENT_NAME']);
            $filePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['LOCAL_RESULT_DOCUMENT_NAME'];
            file_put_contents($filePath, $changedPdfData);
            echo "Downloaded: " . $filePath . "\n";
        }

        private function _init_table() {
            $numOfCols = 5;
            $numOfRows = 5;
        
            $headerTextState = [
                "font" => "Arial Bold",
                "fontSize" => 11,
                "foregroundColor" => ["a" => 0xFF, "r" => 0xFF, "g" => 0xFF, "b" => 0xFF],
                "fontStyle" => "Bold"
            ];
        
            $commonTextState = [
                "font" => "Arial Bold",
                "fontSize" => 11,
                "foregroundColor" => ["a" => 0xFF, "r" => 0x70, "g" => 0x70, "b" => 0x70]
            ];
        
            $table = new \Aspose\PDF\Model\Table();
            $table->setRows([]);
            $table->setColumnWidths(str_repeat(" 70", $numOfCols));
        
            $borderTableBorder = new \Aspose\PDF\Model\GraphInfo();
            $borderTableBorder->setColor(["a" => 0xFF, "r" => 0x00, "g" => 0xFF, "b" => 0x00]);
            $borderTableBorder->setLineWidth(0.5);
        
            $table->setDefaultCellBorder([
                "top" => $borderTableBorder,
                "right" => $borderTableBorder,
                "bottom" => $borderTableBorder,
                "left" => $borderTableBorder,
                "roundedBorderRadius" => 0
            ]);
            $table->setLeft(150);
            $table->setTop(250);
        
            for ($rowIndex = 0; $rowIndex < $numOfRows; $rowIndex++) {
                $row = new \Aspose\PDF\Model\Row();
                $row->setCells([]);
        
                for ($colIndex = 0; $colIndex < $numOfCols; $colIndex++) {
                    $cell = new \Aspose\PDF\Model\Cell();
                    $cell->setDefaultCellTextState($commonTextState);
        
                    if ($rowIndex == 0) {
                        $cell->setBackgroundColor(["a" => 0xFF, "r" => 0x80, "g" => 0x80, "b" => 0x80]);
                        $cell->setDefaultCellTextState($headerTextState);
                    } else {
                        $cell->setBackgroundColor(["a" => 0xFF, "r" => 0xFF, "g" => 0xFF, "b" => 0xFF]);
                    }
        
                    $textRect = new \Aspose\PDF\Model\TextRect();
                    $textRect->setText($rowIndex == 0 ? "header #" . $colIndex : "value #(" . $rowIndex . "," . $colIndex . ")");
                    $cell->setParagraphs([$textRect]);
        
                    $row->getCells()[] = $cell;
                }
                $table->getRows()[] = $row;
            }
            return $table;
        }

        public function addTableOnPage() {
            $newTable = $this->_init_table();

            $resultTabs = $this->pdfApi->postPageTables($this->configParams['PDF_DOCUMENT_NAME'], $this->configParams['PAGE_NUMBER'], [$newTable]);
        
            if ($resultTabs->getCode() == 200) {
                echo "New table successfully appended.\n";
                var_dump($newTable);
            } else {
                echo "Unexpected error: can't append new table !!!\n";
            }
        }

    function main() {
        global $configParams;
        $pdfTables = new PdfTables($configParams);
        try {
            $pdfTables->uploadDocument();
            $pdfTables->addTableOnPage();
            $pdfTables->downloadResult();
        } catch (Exception $e) {
            echo "Error: " . $e->getMessage() . "\n";
        }
    }
 

在PDF中处理表格

表格提供了一种系统化呈现数据的结构化格式,使读者更容易理解和分析信息。它们还增强了文档的视觉吸引力,增加了专业性和组织性。在处理数字或比较数据时,表格通过将相关信息分组在易于阅读的格式中来提高清晰度。此外,表格可以包含实时或动态生成的内容,例如来自数据库或分析仪表板的数据。 使用Aspose.PDF Cloud PHP SDK将表格添加到PDF文档中。

使用我们的PHP库您可以:

  • 添加PDF文档的文本或图像格式的页眉和页脚。
  • 向PDF文档添加表格和印章(文本或图像)。
  • 将多个PDF文档附加到现有文件。
  • 处理PDF附件、注释和表单字段。
  • 对PDF文档应用加密或解密并设置密码。
  • 从页面或整个PDF文档中删除所有印章和表格。
  • 通过ID从PDF文档中删除特定的印章或表格。
  • 替换PDF页面或整个文档中的单个或多个文本实例。
  • 广泛支持将PDF文档转换为各种其他文件格式。
  • 提取PDF文件的各种元素并优化PDF文档。
  • 你可以试用我们的免费应用在线测试功能。