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; ?>
Related
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.
I use mdpf plugin. And I create table of content follow this.
I create html view in pdf_frm01.php file
<html>
<body>
<!-- focus this and I want in same page -->
<div class="toc">
<div>Table of content</div>
<tocpagebreak />
</div>
<!-- -------- --->
<div style="page-break-before : always">
<tocentry content="1. bbbbbbbbb" />
<div>page 2</div>
</div>
<div style="page-break-before : always">
<tocentry content="2. cccccccccc" />
<div>page 2</div>
</div>
</body>
</html
And I create pdf.php for control mpdf action
$pdf_custom = getcwd().'/lib/mpdf/mpdf.php';
require_once($pdf_custom);
$pdf = new mPDF();
$html = $this->load->view('pdf_frm01.php',$data,true); //load text from file
$pdf->writeHTML($html);
$pdf->Output("frm_01",'I');
But. I have a problem.
I want to create a custom table of content style.
I want Table of content (text) on top the table of content.
In table of content. I want to change font-family, font-size, line-height etc.
How to create a custom content table style? or can you recommend me?
Read documentation: https://mpdf.github.io/what-else-can-i-do/table-of-contents.html
you can add custom CSS:
$mpdf->WriteHTML('style.css'), 1);
to change default style TOC. But the problem is that some of custom style not working - I cannot remove "bold" from TOC font.
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.
I want building a form like that:
<div> test </div>
// show me all categories from table
<div class="section">
{{#categories}}
{{{.}}}
{{/categories}}
</div>
// show me all books from table>
<div class="section">
{{#books}}
{{{.}}}
{{/books}}
</div>
Each books or categories has div tag.
i want that each tag start on new page.
Can you tell me how can i insert new page in mustache.
Best Regards,
I found something:
From html is posible, something like <p style="page-break-after:always;"></p>.
After this line, new page is made.
I am currently new to php.I am trying to display results from a database to the same html page.However am not using a form for searching,the search is universal.How can i display the results in same page?
For example heres a part of my html page
<body>
<div class="div1>
button to display results
</div>
<div class="div2">
</div>
and in my php everything is working fine.However i want when a user clicks the "button to display results" link,the results from the database should be displayed in div2.Help will be highly appreciated.
Try this: it will cause the page to refresh passing 1 to the query string to indicate whether to display or not
<div class="div1>
button to display results
</div>
<div class="div2">
<?php
$display = $_GET['display'];
if($display)
{
//do some dispaly here
}
?>
</div>