0

我需要在 pdf 文件中导出 2000 万条数据。我在我的 yii2 应用程序中使用了“WkHtmlToPdf”包(https://github.com/mikehaertl/phpwkhtmltopdf )。我使用过 Mongo 数据库,我创建了一个报告模块,我需要在其中导出包含大数据的 pdf。代码运行良好,但写入 1,00,000 条需要花费太多时间,大约需要 10 分钟,因此 2000 万条大约需要 20 小时,因此我们正在寻找写入 2000 万条数据的解决方案。

以下是我从 mongo 获取数据的查询:

$query = CannedReport::find()->where(['isParent'=>1]);
    $this->load($params);
    if (in_array(Yii::$app->user->identity->user_type, ['tenant', 'subtenant'])) {
        $query->andWhere(['tm_id' => (string)Yii::$app->user->identity->tm_id]);
        // $match['tm_id'] =(string)Yii::$app->user->identity->tm_id;
    }
    $query->andWhere(['!=', 'callstatus', 'completed']);
    $query->andWhere(['hangup' => 'ORIGINATOR_CANCEL']);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => false,
        'sort' => [
            'defaultOrder' => ['start_epoch' => SORT_DESC],
        ],
    ]);

    if (isset($this->em_id) && !empty($this->em_id)) {
        $query->andFilterWhere(['in', 'em_id', $this->em_id]);
    }
    foreach (['start_epoch', 'end_epoch'] as $epoch) {
        if ($epoch == 'start_epoch') {
            if (isset($this->start_epoch) && !empty($this->start_epoch)) {
                $date = explode(' - ', $this->start_epoch);
            } else {
                $date[0] = date('Y-m-d H:i:s', strtotime('today - 31 days'));
                $date[1] = date('Y-m-d') . " 23:59:59";
                $this->start_epoch = implode(' - ', $date);
            }
        } else {
            $date = explode(' - ', $this->{$epoch});
        }
        if (isset($date[0]) && isset($date[1]) &&
            Yii::$app->helper->validateDate($date[0], 'Y-m-d H:i:s') && Yii::$app->helper->validateDate($date[1],
                'Y-m-d H:i:s')
        ) {
            $query->andFilterWhere([
                '>=',
                $epoch,
                (string)Yii::$app->helper->currentTimezone($date[0], 'Y-m-d H:i:s')
            ]);
            $query->andFilterWhere([
                '<=',
                $epoch,
                (string)Yii::$app->helper->currentTimezone($date[1], 'Y-m-d H:i:s')
            ]);
        }
        unset($date);
    }
    return $dataProvider;

pdf生成代码

$pdf = new Pdf(array(
        'binary' => '/usr/local/bin/wkhtmltopdf',
        'ignoreWarnings' => true,
        'commandOptions' => array(
            'useExec' => true,      // Can help on Windows systems
            'procEnv' => array(
                'LANG' => 'en_US.utf-8',
            ),
        ),
    ));

    $options = array(
        'dpi' => 96,
        'image-quality' => 100,
        'disable-smart-shrinking',
        'no-outline',            
    );
    $pdf->setOptions($options);
    $pdf->addPage($tableHtml);
    $detailHeadingHtml = '<div class="row" style="padding-top: 5px;"><h3 style="text-align: center; padding: 5px;">Detailed Records-Call Direction Report</h3><div>';
    $pdf->addPage($detailHeadingHtml . '' . $abandonedCallsHtml . '' . $completedCallsHtml . '' . $failedCallsHtml . '' . $transferredCallsHtml . '<table><tr><td align="center"><b>Total Count:</b></td><td align="center"><b>' . $dataProvider->query->count() . '</b></td></tr></table>');

   if (!$pdf->send($fileName)) {
        $error = $pdf->getError();
        echo $error;
    }
    exit();

是否有任何优化的方法可以使用 yii2 和 mongo 创建具有如此大数据的 pdf?

4

0 回答 0