【问题标题】:Dompdf converts fine on local but not on production serverDompdf 在本地但不能在生产服务器上正常转换
【发布时间】:2014-06-30 13:40:01
【问题描述】:

我正在使用带有 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

【问题讨论】:

  • 您也可以发布生成的 HTML 吗?乍一看,我没有看到任何表明问题的东西。也许是损坏的字体文件?你试过不同的字体吗?

标签: laravel laravel-4 dompdf pdf-conversion


【解决方案1】:

有两种可能的原因:

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

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

【讨论】:

  • 他们都输出这个: string(5) "ASCII" 它到底在哪里抓取字体?喜欢目录?我查看了那个包 github 页面,它说它存储在您的 app/storage/fonts 文件夹中,并且该目录中的内容在我的 live 和 prod 服务器上都是相同的......错误在 prod 服务器上。当我在 URL 中查看它时,它显示为空白,当我将文件拉到我的计算机上时,它显示了奇怪的措辞..
猜你喜欢
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2018-03-07
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多