How to fix iframe rendering of Dompdf output in WordPress admin? - php

I'm trying to render a custom pdf invoice with the help of the Dompdf library in my custom post type admin editing screen. I've setup a metabox to show the iframe with the pdf.
This is the code I'm using:
function invoice_metabox_callback() {
require plugin_dir_path( __FILE__ ) . '/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf;
$dompdf->loadHtml('Hello world');
$dompdf->render();
$output = $dompdf->output();
//file_put_contents(plugin_dir_path( __FILE__ ) . '/pdf-test.pdf', $output);
echo '<iframe src="data:application/pdf;base64,' . $output . '"></iframe>';
}
The iframe is loading, but the pdf viewer is giving the following error: ERR_INVALID_URL
Any idea?
Thanks in advance, have a great day.

Related

Make part of PDF start on odd or even page in Dompdf

I convert html to pdf.
Is it possible to make a part of the PDF start only on an odd or even page, for example, using the page-break-after parameter?
Thanks!
This is my solution that works for me.
This allows new pages to be output from odd.
If you need output new pages from even, change '$PAGE_NUM%2 != 0' to '$PAGE_NUM%2 == 0'
$html ='<body>';
$html .='here is my html';
$html.='<script type="text/php">
if ( isset($pdf) ) {
if($PAGE_NUM%2 != 0) //add empty page if odd one
{
$pdf->new_page();
}
}
</script>';
$html.='<div style="page-break-before: always;"></div>'; //start new page
$html .='this is my html which will start at odd page';
$html .='</body>';
$options = new Options();
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->set_option("isPhpEnabled", true);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();

Changing output markup of an media file

I am creating a wordpress theme for a friend and am trying to add some markup before and after an image that has been inserted using the media button on a post.
Here is what I have so far but it's giving me an error. Any suggestions or help would be much appreciated!
add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
$html1 = 'before ... ';
$html2 = 'after ...';
$content = preg_replace('/(<a.*?)><(img.*?)/', '$1>' . $html1 . '<$2>' . $html2, $content);
return $content;
}
Thanks!
Ponte

WordPress simple Image Shortcode

I am a newbie, please help me with creating a wordpress image shortcode, as simple as:
[img src=""]
Which shows its thumbnail (thumbnail width=100%), links to OR opens the same source image, when clicked.
I tried searching but could not find in existing plugins, please guide me if any.
Please guide me thoroughly for every copy paste in function.php or anywhere else.
// Add Shortcode
function img_shortcode($atts)
{
// Attributes
$atts = shortcode_atts(
[
'src' => '',
'link_to_img' => '',
], $atts, 'img'
);
$return = '';
if ($atts['link_to_img'] == 'yes')
{
$return = '<a href="' . $atts['src'] . '">
<img src="' . $atts['src'] . '"/>
</a>';
}
else{
$return = '<img src="' . $atts['src'] . '"/>';
}
// Return HTML code
return $return;
}
add_shortcode('img', 'img_shortcode');
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
USAGE
Without link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]');
Without link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]
With link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]');
With link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]
Hope this helps!
The Gallery feature allows wordpress to add one or more image galleries to your posts and pages using a simple Shortcode
[gallery ids="729,732,731,720"]
enter link description here

unable to print pdf (MPDF)

I am trying to print my content to PDF using Mpdf tool . The code is working in localhost , But when i tired same code in server its not working giving some eroor "mPDF error: Some data has already been output to browser, can't send PDF file".
My code is :
<?php
$address = "banglore rt nagar";
$template_data = " hello this is test ##ADDRESS## adress";
$template_data = str_replace('##ADDRESS##', $address , $template_data);
ob_end_clean();
include 'MPDF57/mpdf.php';
$mpdf=error_reporting(E_STRICT);
$mpdf=new mPDF('win-1252','A4','','',15,10,16,10,10,10);
$mpdf->Bookmark('Start of the document');
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($template_data);
$mpdf->Output();
exit();
?>
i have tried lot of solutions . but nothing works good . any help ?
Use ob_start(); after the <?php tags . hope it will helps you .
more ohttp://php.net/manual/en/function.ob-start.php

joomla get content in onBeforeCompileHead function

I'm developing a Joomla system plugin. I need to add javascript and css files if the certain [myshortcode] shortcode found in content.
How can I get the content in onBeforeCompileHead function
function onBeforeCompileHead() {
$document = JFactory::getDocument();
//add scripts only if shortcode found
...
$document->addStyleSheet($cssFile, 'text/css', null, array());
The $content = JResponse::getBody(); does not work.
Thanks
Try this,
For style sheet,
$document = JFactory::getDocument();
$document->addStyleSheet('http://domain.com/templates/css/style.css');
For Javascript File
$document = JFactory::getDocument();
$document->addScript('full url');
Also you can do like this,
$document = JFactory::getDocument();
// Add Javascript
$document->addScriptDeclaration('
window.event("domready", function() {
alert("An inline JavaScript Declaration");
});
');
// Add styles
$style = 'body {'
. 'background: #00ff00;'
. 'color: rgb(0,0,255);'
. '}';
$document->addStyleDeclaration($style);
If this case any jQuery issue you can try addCustomTag option too like here
Hope its helps..

Categories