0

我正在使用带有 dompdf 包的 Laravel 4:https ://github.com/barryvdh/laravel-dompdf

当我在本地生成报告并将其转换为 PDF 时,一切都很好并且显示良好,但是当我在生产服务器上执行相同的操作时,它会显示随机字母,其中有动态或静态内容。

本地与生产的屏幕截图:

http://s28.postimg.org/u5zk3pc19/report_diff.png

这是创建 PDF 的代码:

/**
 * Create PDF
 *
 */
public function createPdf( $reportData )
{
    if( $this->validate() )
    {
        // Get Final Data Information
        $btu_hp = static::getBtuHp( $reportData['booth_cfm'], $reportData['cure_temp_hp'], $reportData['outside_temp'] );
        $btu_current = static::getBtuCurrent( $reportData['booth_cfm'], $reportData['bake_temp_current'], $reportData['outside_temp'] );

        $reportData['energy_percentage_per_unit'] = static::getEnergyPercentagePerUnit( $btu_hp, $btu_current );
        $reportData['energy_dollar_per_unit'] = static::getEnergyDollarPerUnit( $reportData['cost_per_therm'], $reportData['bake_time_current'], $reportData['cure_time_hp'], $btu_current, $btu_hp );
        $reportData['time_savings_per_unit'] = static::getTimeSavingsPerUnit( $reportData['bake_time_current'], $reportData['cure_time_hp'] );
        $reportData['time_savings_per_year'] = static::getTimeSavingsPerYear( $reportData['time_savings_per_unit'][0], $reportData['units_per_day'], $reportData['production_days'] );
        $reportData['labor_dollar_per_year'] = static::getLaborDollarPerYear( $reportData['labor_rate'], $reportData['time_savings_per_year'][0] );
        $reportData['energy_dollar_per_year'] = static::getEnergyDollarPerYear( $reportData['energy_dollar_per_unit'][0], $reportData['units_per_day'], $reportData['production_days'] );

        $view = View::make('pages.report.hp-report.print', array('report' => $reportData));

        if( ! $this->saveAsPdf($view, $this->generateFileName()) )
        {
            return false;
        }

        return true;
    }

    return false;
}

/**
 * Save report as PDF
 * @param html         HTML of PDF
 * @param fileName     Name of File
 *
 */
public function saveAsPdf( $html, $fileName = null )
{
    if(is_null($fileName))
        $fileName = $this->generateFileName();

    $htmlPath = $this->reportDirectory.'/'.$fileName.'.html';
    $pdfPath = $this->reportDirectory.'/'.$fileName.'.pdf';

    file_put_contents( $htmlPath, $html );

    // set recent PDF to name of PDF
    $this->recentReportFile = $fileName . '.pdf';
    return PDF::loadFile($htmlPath)->save($pdfPath);
}

/**
 * Get most recent uploaded PDF
 *
 */
public function getRecentPdf()
{
    return $this->recentReportFile;
}

/**
 * Generate file name for PDF
 *
 */
public function generateFileName()
{
    return Auth::user()->id . '_hp_' . str_random(10) . '_' . time();
}

一切都写得很好,它使用正确的模板并具有样式......只有静态内容和动态内容(用 PHP 变量写出的值)显示不好,尽管你可以看到一些静态内容,如节能和这样的打印很好.

是否有原因这可能会在实时服务器上全部混乱,但不是本地服务器?

这是正在抓取的视图的 HTML(注入 php 变量的 HTML): http: //pastebin.com/5bMR6G2s

这是我的 dompdf 配置文件:http: //pastebin.com/Ld6MQckG

4

1 回答 1

2

这有两个可能的原因:

  1. 生产服务器上缺少字体。确保您在生产站点上安装了正确的字体。

  2. 字符编码问题。我不确定问题出在哪个站点(开发/实时),但可能是一个正在输出 UTF-8 而另一个不是。您可以尝试通过使用mb_detect_encoding检测 dev 和 live 上的输入文件的编码来解决这个问题,看看它们是否不同。如果是,则在转换为 PDF 之前使用mb_convert_encoding 。

于 2014-06-30T15:30:08.833 回答