DOMPDF outputs bad html on Linux - php

UPDATE1:
Works pretty well on Ubuntu but not on CentOS.
I have a issue with the DOMPDF on CentOS Linux VPS,
I output an HTML with css on my WAMPP and works pretty well:
But when i upload the same code, the same html it shows as this:
I tried everything like check encoding, inline css, enabling remote but still no way to get this running, here is my code:
require_once(APP . 'vendors' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new Dompdf();
def("DOMPDF_ENABLE_REMOTE", true);
$_GET['print'] = 1;
$dompdf->set_paper("A4", "portrait");
$this->output = '';
$this->layout = 'none';
$old_output = $this->output;
$this->render('innobra_pdf');
$new_output = $this->output;
$dompdf->load_html($new_output);
// $dompdf->load_html( utf8_decode($new_output), Configure::read('App.encoding') );
$dompdf->render();
$output = $dompdf->output();
header('Content-type: application/pdf');
echo $output; die();
What i'm doing wrong ?
Thanks

If you have copied to another server, try deleting the file
dompdf/lib/fonts/dompdf_font_family_cache.php

Related

HTML2PDF 504 Gateway Timeout

I am trying to convert HTML 2 PDF with this library and its converting fine as per my expectations.
However, When I convert with more data, its giving me an error saying 504 Gateway Timeout. Here is the error screenshot what I am getting.
In local server its working fine. I have the same server in my local and my live (Linux). The only problem is I am getting when I try to produce PDF with long data on live server.
I researched and found out that to increase php execution time and other settings. Hence I try to put below code in my .php file.
ini_set('max_execution_time', 60000);
ini_set('post_max_size','128M');
ini_set('upload_max_filesize','128M');
I even try to set max_execution_time to 0 and -1 but yet its not working for me. After setting this values, I even printed the updated values with phpinfo(), the values are overwriting but I am having the same 502 Gateway timeout error. Here is small chunk of code just in case you want to see.
<?php
ini_set('max_execution_time', 60000);
ini_set('post_max_size','20M');
ini_set('upload_max_filesize','8M');
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/templateInfo.php';
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
use technobrave\brochuresettings\Models\Brochuresettings as BS;
use Technobrave\Transactions\Models\Transactions as TR;
use Technobrave\Offices\Models\Offices;
use technobrave\themesettings\Models\ThemeSetting as TS;
use Technobrave\Team\Models\Team;
class generateTemplate {
public $theme = "";
public $theme_settings = array();
public function __construct($templateId, $resolution , $theme ,$pdf_sections = array(),$openFile = false, $finalPdfFile = null) {
$this->getBrochureTransactionData = BS::first();
$this->getPdfSection = $pdf_sections;
$this->theme_settings = TS::first();
$this->theme = $theme;
$this->baseUrl = url(Config::get('cms'));
$this->teamPageName = $this->baseUrl . '/our-team';
$this->capabilitiesPageName = $this->baseUrl . '/capabilities';
$this->getFooterText = $this->getFooterText();
$getTeamId = (isset($_GET['teamId']) && !empty($_GET['teamId'])) ? $_GET['teamId'] : "";
$this->uniquePath = __DIR__ . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
$templatePath = __DIR__ . DIRECTORY_SEPARATOR . 'regency_template' . DIRECTORY_SEPARATOR . $getTeamId . DIRECTORY_SEPARATOR . $templateId . '.php';
$templateInfoText = new templateInfo($templateId, $this->uniquePath, $getTeamId);
$this->customImagePath = $this->uniquePath;
foreach ($templateInfoText->defaultValues as $key => $value) {
$this->{$key} = $value;
}
$template = file_get_contents($templatePath);
try
{
$html2pdf = new Html2Pdf('L','A4', 'en', true, 'UTF-8', array(0, 0, 0, 0));
$html2pdf->Addfont('perpetua');
$html2pdf->Addfont('montserratbold');
$html2pdf->Addfont('montserratmedium');
$html2pdf->Addfont('montserratregular');
$html2pdf->Addfont('montserratsembold');
$html2pdf->Addfont('montserratitalic');
$html2pdf->writeHTML($template, false);
$html2pdf->Output('regency_corporate_brochure.pdf', 'D');
} catch (Html2PdfException $e) {
$formatter = new ExceptionFormater($e);
echo $formatter->getHtmlMessage();
}
}
}
I tried to preview how my HTML is generating and its generating without any error.
$html2pdf->writeHTML($template, true);
So basically I am facing server issue here as per my understanding so far.
Can someone guide me what should I do from here on to solve this issue.
After a hard debugging I found that my problem was that I was including an external image in the PDF and the server can't reach the server image (server access only trough white-list IP).

mPDF not generating PDF in firefox

I am using mPDF library for creating PDF using html data.it works fine in google chrome,but in firefox it displays nothing.No files downloaded and no errors in console.
Here is my code :
require_once( $destination_path."/mpdf/mpdf.php");
$target_path = $destination_path . '/uploads/reports/';
$htmlData='<span>Sample Html Content</span>';
try {
$pdf= new mPDF();
$pdf->debug = true;
$pdf->SetFooter("MyApp" . '|{PAGENO}|' . date(DATE_RFC822));
$pdf->WriteHTML($htmlData);
$pdf->Output($fileOutputPath, 'F');
}catch (Exception $e) {
echo 'Caught exception: ', $e, "\n";
}
i am tried ob_clean() and headers for solve the issue,but it remains same .
Anyone knows how to solve this issue?

DOMPDF Wordpress nothing happens

I have a problem with DOMPDF integration into Wordpress.
Here are the steps done:
Installed DOMPDF 0.6.1
into the themedir/dompdf/
Created path /dompdf/lib/php-font-lib/classes where php lib is to be
imported
Imported the "FontLib" folder contents
(version 0.4) into the
/dompdf/lib/php-font-lib/classes
Here is the code:
require_once(echo get_template_directory_uri()."/dompdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream(echo get_template_directory_uri()."/dompdf/testpdf.pdf");
Outputs nothing, I have also tried stream("testpdf.pdf"); as the last line but no success.
Any ideas? Thank you.
Try this
include get_template_directory() . '/dompdf/dompdf_config.inc.php';
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html( $html );
$dompdf->render();
file_put_contents( get_template_directory() . "/dompdf/testpdf.pdf");

file download in yii shows file not exist

I am trying to write a script in Yii for downloading files. view file isecho CHtml::link('Major Oilseeds: World Supply and Distribution.','downloadpdf', array('class'=>'btn btn-darkorange')); and the controller code is $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
echo $path;
if(file_exists($path)){
Yii::app()->getRequest()->sendFile( 'Major_Oilseeds.csv' , file_get_contents($path) );
}
else{
echo '<br>File not found';
} the code echo $path dispalys the location as http://localhost/projectv2/download/Major_Oilseeds.csv and the download folder contains the file named "Major_Oilseeds.csv" but its always showing "File Not Found" Error. plz somebody help me to solve this. I have also tried the code $path = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/download/Major_Oilseeds.csv';
// $filename = 'Major_Oilseeds.csv';
header('Content-Disposition: attachment; charset=UTF-8; filename="'.$path.'"');
$utf8_content = mb_convert_encoding($content, "SJIS", "UTF-8");
echo $utf8_content;
Yii::app()->end();
return; but its also not working :-(
file_exists can only work with local files. In your case you are trying to use file_exists with a URL.
You can find a workaround here: http://uk1.php.net/file_exists (please search for "http" in this page)

Image is being shown as HTML but no image is shown for DOMPDF()

I try to show image as follows:
function part_profile_record_pdf_form()
{
$sql = "SELECT * FROM part_profile WHERE part_id=" . arg(2);
$query = db_query($sql);
$dataFormDb = db_fetch_object($query);
$url = '../../../' . $dataFormDb->image;
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output = "<style>" . file_get_contents($css_file) . "</style>";
$output .= "<html><body ><div id=\"wrapper\">";
$output.= '<img height="156" width="128" id="img_profile" src="'.$url.'">';
$output.="</div></body></html>";
return $output;
}
I can see the image by the following function:
function part_profile_make_pdf() {
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output= part_profile_record_pdf_form();
print $output;
}
But when I try to make pdf as follows , no image is shown giving 'x' and 'image not readable or empty':
function part_profile_make_pdf() {
$css_file = drupal_get_path('module', 'part_profile') . '/css/part_profile_pdf.css';
$output= part_profile_record_pdf_form();
// print $output;
require_once(realpath(".") . "/sites/all/libraries/dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
if (isset($_SESSION['indv_report_file_name']) && !empty($_SESSION['indv_report_file_name'])) {
$filename = $_SESSION['indv_report_file_name'];
} else {
$rand_val = rand(0, 1000);
$filename = "eureka_" . $rand_val . ".pdf";
$_SESSION['indv_report_file_name'] = $filename;
}
$dompdf = new DOMPDF();
$dompdf->load_html($output);
// $dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'a4');
// "a4" => array(0,0,595.28,841.89),
$dompdf->set_paper(array(0, 0, 595.28, 420), "portrait"); // 12" x 12"
$dompdf->render();
$dompdf->stream("participant-profile.pdf", array("Attachment" => false));
exit(0);
}
I already set DOMPDF_ENABLE_REMOTE to true in my config dompdf_config.inc.php. have any idea. Thanks in advance.
After a little testing in my own DOMPDF environment, I reckon this will be due to the relative path to your image. Currently you're using
$url = '../../../' . $dataFormDb->image;
Which is fine for the preview in HTML, the browser can find the image.
But when DOMPDF tries to process the HTML and find the image, it's doing so from a system perspective, like PHP would if you were doing a file_exists(). So you need to add the full path:
$url = $_SERVER['DOCUMENT_ROOT'] . '/path/to/dir/' . $dataFormDb->image;
or just hardcode it
$url = '/home/username/sitename/public_html/images/' . $dataFormDb->image;
So you might need some logic to say: am I doing a preview? Then use relative path. Or am I doing a PDF render? Then use full path. But I'll leave that to you!

Categories