Error rendering data from database with dompdf - php

I'm using DOMPDF library for made a prices list. I installed the library and try with simple pdf and all works ok. In my html template, i'm rendering data from a mysql table.
This is my HTML :
<!-- Html pure code here-->
<?php for ($i=0; $i < count($products); $i++) { ?>
<section class="row">
<div class="col-md-4">
<h3><?=$products[$i]['codigo']?></h3>
</div>
<div class="col-md-8">
<h3><?=$products[$i]['titulo']?></h3>
</div>
</section>
<section class="row">
<div class="col-md-4">
<img src="<?=$products[$i]['img']?>" />
</div>
<div class="col-sm-8 data-producto">
<div class="descripcion">
<?=$products[$i]['descripcion']?>
</div>
<div class="precios">
<span class="precio">
</span>
</div>
</div>
</section>
<?php } ?>
And that's my controller code :
$categorias = $this->post('id_categoria');
$productos = new Modelos\Producto();
// This method make the query and return an array with data.
$products =
$productos->get(
['id_categoria'=>$categorias],
null,
'categoria asc,titulo asc'
);
//Helpers\Debug::imprimir($listadoProductos);
ob_start();
include 'Layout/electron/listado.tpl.php';
$html1 = ob_get_clean();
if (ob_get_length()) ob_end_clean();
$pdf = new Dompdf();
//exit($html1)
$pdf->loadHtml($html1);
$pdf->render();
$pdf->stream();
The error : The pdf takes a lot of time to be created. It takes up 3 minutes and sometimes simply it doesn't.
If a print the pdf without the foreach loop. The pdf is created correctly. If y print only the html, without use dompdf, only in the navigator. the script run in a second. I need to do something more?
The products table has 45 products only.
Thanks.

The bug was because I was including bootstrap css file and DOMPDF does not support it correctly. Then I removed bootstrap and edit my file using a custom css and tables instead bootstrap's grid.
That's all.

Related

Unable to return output of PHP function, while echo displays string

I am using a PHP function to output a string of HTML. I was initially just echoing the output, but my HTML was displaying too high on the page when I did that. In the past, using ob_start() and returning ob_get_clean() has solved this issue, yet it is not working in this case. In fact, when I use this method, no HTML is output at all. When I use var_dump() on the variable, it is confirming that my HTML string is indeed being saved into the object, yet return still outputs nothing.
<?php
add_shortcode( 'slider-display', 'slider_display' );
function slider_display(){
$slider_html = '';
ob_start();
?>
<!-- Slider main container -->
<div class="swiper-container one-column-swiper" id="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
</div>
</div>
<?php
$slider_html = ob_get_clean();
return $slider_html;
} //end function
Any help is very much appreciated!

Wordpress image caption doesn't display correctly

I have a Wordpress page, in which my images appears on left and my text appears on right. The images fall down under the text when the page is loaded on mobile screen. I have a PHP code to achieve all of that, basically it switchs image and text places, and attributes left or right to each.
But since this is in place, I have trouble adding images' captions. When I want to add images' captions in my Wordpress admin, the caption doesn't come under the image but it comes up my text article (so, on the right part of the page). I guess my PHP code attributes automaticaly this place to every text.
I need the images' captions to be under my images, and I guess this has a lot to do with that PHP code you can see below. But I don't know how to fix that.
Here is my PHP:
<?php if (have_posts()): ?>
<div class="container">
<?php while(have_posts()): the_post(); ?>
<div class="row">
<div id="content_post">
<div class="col-lg-push-5 col-lg-6 col-md-push-6 col-md-6 col-sm-12 col-xs-12 ">
<?php
//get content from back office , filter imgs except featured img to place them later in <div id='img_content_right'>
$content2 = get_the_content();
$removed_imgs = array();
$content2 = preg_replace_callback('#(?!<img.+?id="featured_img".*?\/>)(<img.+? />)#',function($r) {
global $removed_imgs;
$removed_imgs[] = $r[1];
return '';
},$content2);
//add filter to the_content to strip images except img_menu et featured_img
add_filter('the_content', 'strip_images',2);
function strip_images($content){
$content=preg_replace('/(?!<img.+?id="img_menu".*?\/>)(?!<img.+?id="featured_img".*?\/>)<img.+?\/>/','',$content);
return $content;
}
the_content();
?>
</div>
</div>
<div class="col-lg-pull-6 col-lg-5 col-md-pull-6 col-md-6">
<div id='img_content_right'>
<?php
//append the images stripped from content
foreach($removed_imgs as $img){
echo $img; } ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
To appropriately handle the caption you need to get the attachment post object and then look at the post_excerpt.
There's a function in WordPress get_attached_media, which will list all of the images within a posts content. This be easier to iterate over then using regex to find the images.

Fatal error: Some data has already been output, can't send PDF file

so this is my code
<body>
<section id="container" >
<?php require 'inc/header.php';?>
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<!-- 1ere ETAPE -->
<h1 style="text-align:center ;"> Rénitialisation </h1>
<div class="etape1">
<?php
require('inc/reni/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"nom");
$pdf->Cell(30,7,"prenom");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
include ('inc/reni/db.php');
$sql="SELECT * FROM table1";
$result = $bdd->query($sql);
while($rows=$result->fetch())
{
$nom = $rows[0];
$prenom = $rows[1];
$pdf->Cell(25,7,$nom);
$pdf->Cell(30,7,$prenom);
$pdf->Ln();
}
$pdf->Output();?>
</div>
</div>
</div>
</div><!-- /col-md-12 -->
</div><!-- /row -->
</section><! --/wrapper -->
</section><!-- /MAIN CONTENT -->
</section>
<?php require 'inc/script.php';?>
</body>
The error is : Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file.
i try everything like add ob_end_clean(); ob_start ();ob_end_flush();
and when i do this my page is empty the Pdf does not display
What you try to use the result it's an octet stream which sends the PDF file to your Browser to download the file. In this case you can't have an output before. Create a single file and output that PDF file without writing anything before. Then you can download the file.
That is what the error message tells you. Don't output anything before the output of the PDF file.
You're already outputting something.
Usually there's an echo somewhere, or you have php code like this:
<HTML>
<?php // do my pdf thing ?>
If anything is in front of your pdf output, wether it be a space, some html, an echo, a print, you will get this error.
Even a space after a php closing tag of a pdf that was included can cause this.
<?php
class PDFWriter() {
// some code
}
?> <-- space here
<?php include('pdfwriter.php');
That's why you should omit the closing tags on pure php files so that doesn't happen.
In your case you're adding all these divs
<body>
<section id="container" >
<?php require 'inc/header.php';?>
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<!-- 1ere ETAPE -->
<h1 style="text-align:center ;"> Rénitialisation </h1>
<div class="etape1">
And that violates the no output sending requirement.

mPDF - Content from specific div that is visible + add a save button

So I'm trying to set mPDF on my site, but the thing is, on the page that I want to create pdfs on, I get all of the content inside divs based on the posts count. So it looks something like this:
<div id="all-posts">
<div id="some-post">
content
</div>
<div id="some-other-post">
content
</div>
<div id="post-with-long-title">
content
</div>
</div>
And then I have a select, with options values which are equal to the div IDs inside all-posts. And when you pick an option from the select, that div is showed. Now what I need to do is show a Save PDF button, and when that is clicked, get the content from the div that is currently showing (based on whats selected in select tag) and save the .pdf. Is something like this even possible?
Update:
<?php include("mpdf/mpdf.php");
$mpdf=new mPDF();
ob_start(); ?>
<div id="all-posts">
<div id="some-post">
content
</div>
<div id="some-other-post">
content
</div>
<div id="post-with-long-title">
content
</div>
</div>
// Now collect the output buffer into a variable
<?php $html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit; ?>

php in foundation modal

I am trying to pull some information from my database and put it in a modal. I went to the foundations website and tried to figure it out from their docs section. I dont exactly understand it. So I have a section of my site that allows users to request to delete a song they uploaded. Now if they click the X a modal should pop up and ask to confirm.
<div class="row">
<div class="large-8 column musicup">
<p> <?php echo "No music uploaded..."; ?> </p>
</div>
</div>
<?php
}else{
?>
<h2 style="margin-top:1em;">Music uploaded</h2>
<hr style="opacity:.4;">
<?php
while($row_a = mysql_fetch_array($res))
{
?>
<div class="row">
<div class="large-4 column musicup">
<p><?php echo $row_a['title']; ?></p>
</div>
<div class="large-3 column musicup"><span data-tooltip class="has-tip tip-top" title="<?php echo $row_a['reason']; ?>">
<div class="button <?php echo $row_a['status'];?>"><?php echo $row_a['status'];?></div>
</span></div>
<div class="large-3 column musicup_date">
<p><?php echo date('F j Y',strtotime($row_a['uploaded'])); ?> </p>
</div>
<div class="large-2 column musicup">
<p>X</p>
</div>
</div>
<?php
}
}
}
?>
</div>
So now I have the modal and all the database queries on a new page called song_delete.php.
Here is the code for that:
<?php
include_once "functions.php";
$query = sprintf("SELECT * FROM songs WHERE user_id = %d AND song_id = %d",$_SESSION['user_id'], $_GET['id']);
$res = mysql_query($query) or die('Error: '.mysql_error ());
$row_a = mysql_fetch_assoc($res);
$totalRows_a = mysql_num_rows($res);
?>
<div id="deleteMusic" class="reveal-modal medium">
<h2>Request to delete<span style="color:#F7D745;"> <?php echo $row_a['title']; ?></h2>
<p class="lead">Are you sure you want to delete this song? Please allow 2 full business weeks for deletion.</p>
<span style="float:right;">Cancel
Submit </span>
<a class="close-reveal-modal">×</a>
</div>
Thanks for any help in advance. I appreciate it.
Please dont tell me about the mysql_query and how I should use PDO or MySQLi and OOP i know this, but this site is not currently coded with all that..
OK first things first - its often better to look at the compile source (HTML Source Code) in these cases. Can you do this? From the code you've given it looks fine but without the css/js linking and showing the placement of the reveal code there's no way to tell.
How Foundation Reveals Work
1 - The modal code is placed just before the ending </body>.
2 - It should look something like this:
<div id="myModal" class="reveal-modal">
<h2>Awesome. I have it.</h2>
<p class="lead">Your couch. It is mine.</p>
<p>Im a cool paragraph that lives inside of an even cooler modal. Wins</p>
<a class="close-reveal-modal">×</a>
</div>
3 - Depending on the size you want you can use an extra class of .small (for a reveal size of 30% browser width. Or one of these (taken directly from Foundation Docs)
.medium: Set the width to 40%.
.large: Set the width to 60%.
.xlarge: Set the width to 70%.
.expand: Set the width to 95%.
4 - At this point you can attach data-reveal-id="<id of modal here>" or call the modal via foundation. At this point your modal will popup in all Foundation 4 supported browsers. However you need the javascript files to close it.
5 - Now make sure you have the necessary scripts
<!-- If running version with default scripts -->
<script src="foundation.js"></script>
<script src="foundation.reveal.js"></script>
6 - Then call $(document).foundation() and then via the magical jQuery javascript library it should work as intended :-).
Extras
You can add extras attributes to reveal if you wish this way (List of all the attributes
):
$(document).foundation('reveal',<options here>,<callback>)
Lastly you might want to take the ajax tag off this (you aren't calling any content in asynchronously - it's all compiled at runtime via your server

Categories