php, html and pdf with drupal - php

Having the next PHP code that produce HTML code:
(simplified function, the real one on same idea but longer with loops and so on):
<?php
function show_doc_html() {
$text_to_title = "some text from db";
?>
<html>
<head>
<title>
<?php echo $text_to_title ?>
</title>
</head>
<body>
</body>
</html>
<?
}
I would like to return a PDF to the user, without changing too much of this code. we are working under drupal so we have function that can get html string and convert it to pdf, but the former function doesn't return anything but printing to stdout. Id it possible? or should i rebuild the old function to return string?

Is that what you need?
I have used it in my project. You can just write your markup and inline css in node template using view_mode = 'PDF'

The easiest way was to encapsulate my function with "ob_start()" get all text using "ob_get_contents()", then convert it to pdf.
Something like:
function show_doc_pdf() {
ob_start();
function show_doc_html() ;
$html_var = ob_get_contents();
ob_end_clean();
//use wkhtmltopdf api on $html_var to return pdf
}

Related

Is it possible to change original html text in php?

I am trying to make "manner friendly" website. We use different declination dependent on gender and other factors. For example:
You did = robili
It did = robilo
She did = robila
Linguisticaly this is very simplified (and unlucky) example! I would like to change html text in php file where appropriate. For example
<? php
something
?>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^</div>
<? php something ?>
Now I would like to replace all occurences of different tokens ^characters|characters|characters^ and replace them by one of their internal values according to "gender".
It is easy in javascript on the client side, but you will see all this weird "tokenizing" before javascript replace it.
Here I do not know the elegant solution.
Or do you have better idea?
Thanks for advice.
You can add these scripts before and after the HTML:
<?php
// start output buffering
ob_start();
?>
<html>
<body>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^, but also vital^si|sa|ste^, borko^mal|mala|malo^ </div>
</body>
</html>
<?php
$use = 1; // indicate which declination to use (0,1 or 2)
// get buffered html
$html = ob_get_contents();
ob_end_clean();
// match anything between '^' than's not a control chr or '^', min 5 and max 20 chrs.
if (preg_match_all('/\^[^[:cntrl:]\^]{3,20}\^/',$html,$matches))
{
// replace all
foreach (array_unique($matches[0]) as $match)
{
$choices = explode('|',trim($match,'^'));
$html = str_replace($match,$choices[$use],$html);
}
}
echo $html;
This returns:
html text of the page and somewhere is the word "robil" we tried to
robilo, but also vitalsa, borkomala

How to code PHP function that displays a specific div from external file if div called by getElementById has no value?

Thank you for answering my question so quickly. I did some more digging and ultimately found a solution for grabbing data from external file and specific div and posting it into another document using PHP DOMDocument. Now I'm looking to improve the code by adding an if condition that will grab data from a different div if the one called for initially by getElementById has now data. Here is the code for what I got so far.
External html as source.
<div id="tab1_header" class="cushycms"><h2>Meeting - 12:00pm to 3:00pm</h2></div>
My PHP file calling from source looks like this.
<?php
$source = "user_data.htm";
$dom = new DOMDocument();
$dom->loadHTMLFile($source);
$dom->preserveWhiteSpace = false;
$tab1_header = $dom->getElementById('tab1_header');
?>
<html>
<head>
<title></title>
</head>
<body>
<div><h2><?php echo $tab1_header->nodeValue; ?></h2></div>
</body>
</html>
The following function will output a message if a div id can't be found but...
if(!tab1_header)
{
die("Element not found");
}
I would like to call for a different div if the one called for initially has no data. Meaning if <div id="tab1_header"></div> then grab <div id="alternate"><img src="filler.png" /></div>. Can someone help me modify the function above to achieve this result.
Thanks.
either split up master.php so div1\2 are in a file each or set them each to a var, them include master.php, and use the appropriate variable
master.php
$d1='<div id="description1">Some Text</div>';
$d2='<div id="description2">Some Text</div>';
description1.php
include 'master.php';
echo $d1;
You can't do this solely with PHP includes unless you put the divs into separate files. Look into PHP templating; it's probably the best solution for this. Or, since you're new to the language, try using variables:
master.php
$description1 = '<div id="description1">Some Text</div>';
$description2 = '<div id="description2">Some Text</div>';
board1.php
include 'master.php';
echo $description1;
board2.php
include 'master.php';
echo $description2;
Alternatively, you could use JavaScript, but that might get a little messy.
Short answer is: although it's possible it's probably very bad idea taking this approach.
Longer answer: the solution may turn out to be too complicated. If in your master.php file is only HTML markup, you could read content of that file with file_get_contents() function and then parse it (i.e. with DOMDocument library functions). You would have to look for a div with given id.
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$divs = $doc->getElementsByTagName('div');
foreach ($divs as $div)
{
if( $div->getAttribute('id') == 'description1' )
{
echo $div->nodeValue."\n";
}
}
?>
If your master.php file has also some dynamic content you could do following trick:
<?php
ob_start();
include('master.php');
$sMasterPhpContent = ob_get_clean();
// same as above - parse HTML
?>
Edit:
$tab_header = $dom->getElementById('tab1_header') ? $dom->getElementById('tab1_header') : $dom->getElementById('tab2_header');

how to display an array content inside CKeditor?

I need to display the content of doc file inside CKeditor.
I read the content of doc file & passing it into an array line by line :
$rs = fopen("text.doc", "r");
while ($line = fgets($rs, 1024)) {
$this->data[] = $line . "<BR>";
}
then I create an instance of CKeditor:
include_once("ckeditor/ckeditor.php");
$CKeditor = new CKeditor();
$CKeditor->basePath = '/ckeditor/';
foreach ($this->data as $value) {
//what should I write here
}
$CKeditor->editor('editor1');
the CKeditor work right now & appear on my webpage .. but without any content ?
what should I right inside the foreach to passing array content into the editor ?
please help =(
.doc files are zipped up and cannot be read like this, by line. Consider using PHPWord to get access to the contents inside.
EDIT: Looks like PHPDoc can only write and not read, upon further investigation.
PHP tools are very deficient in this area. Your best bet is to use something like DocVert to do your file conversions on the command line. THEN you could load that document inside CKEditor.
EDIT: after OP's comment:
let's consider it's a txt file ... I need the Ckeditor method
Load your decoded HTML content into a Textarea, and give this textarea an HTML ID or class:
$textarea_content = htmlspecialchars_decode(file_get_contents('text.doc'));
Then, in your HTML, call the CKEditor inside a JavaScript tag to replace the textarea with the editor:
<html>
<head>
<!-- include CKEditor in a <script> tag first -->
<script type="text/javascript">
window.onload = function()
{
CKEDITOR.replace( 'editor1' );
};
</script>
</head>
<body>
<textarea id="editor1" name="editor1"><?php echo $textarea_content ?></textarea>
</body>
The documentation page has a lot more details.

Create DOM elements using Javascript

Hi I am trying to retrieve RSS feed using PHP and javascript.
I can get RSS feeds by using PHP but I want to create DOM elements for RSS feed using javascript. I am not sure how to complete this. Could anyone share some tips? Thanks a lot.
PHP
function parse_rss_feed($url){
$contents= file_get_contents($url);
$xmlStr= simplexml_load_string($contents);
return $xmlStr;
}
function get_rss_feed($xmlStr){
echo '<ul>';
foreach ($xmlStr->item as $node):
//I want to transfer my RSS value to my javascript....
$title=$node->title;
$author=$node->creator;
$desc=$node->description;
endforeach;
echo '</ul>';
}
html
<head>
<script type="text/javascript" src="js/slideshow.js"></script>
</head>
<body>
<?php
include 'getFeed.php';
parse_rss_feed('http://myRSSFeed')
get_rss_feed($xmlStr)
?>
</body>
slideshow.js
var rss=document.createElement('artical');
//How do I get the value from my php...
rss.innerHtml=.......???
Couldn't be easier, createElement().
var el = document.createElement('div')
Edit:
To mix JavaScript with PHP just escape it as you'd usually do but I suggest if you're doing something serious to learn about AJAX.
?>
<script>
var foo = <?php echo 'baz' ?>
</script>
<?php

Making all PHP file output pass through a "filter file" before being displayed

Is there any way for all my PHP and/or HTML file output to be "filtered" before being displayed in the browser? I figured that I could pass it through a global function before it is displayed but I'm stuck on the implementation. Please help.
If there is a better way to achieve the same result, I'd be happy to know.
Thanks.
Check out ob_start which lets you pass a callback handler for post-processing your script output.
For example, PHP includes a built-in callback ob_gzhandler for use in compressing the output:
<?php
ob_start("ob_gzhandler");
?>
<html>
<body>
<p>This should be a compressed page.</p>
</html>
<body>
Here's a fuller example illustrating how you might tidy your HTML with the tidy extension:
function tidyhtml($input)
{
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 200);
$tidy = new tidy;
$tidy->parseString($input, $config, 'utf8');
$tidy->cleanRepair();
// Output
return $tidy;
}
ob_start("tidyhtml");
//now output your ugly HTML
If you wanted to ensure all your PHP scripts used the same filter without including it directly, check out the auto_prepend_file configuration directive.
You can use output buffering and specify a callback when you call ob_start()
<?php
function filterOutput($str) {
return strtoupper($str);
}
ob_start('filterOutput');
?>
<html>
some stuff
<?php echo 'hello'; ?>
</html>
You can use PHP's output buffering functions to do that
You can provide a callback method that is called when the buffer is flushed, like:
<?php
function callback($buffer) {
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
In that case output is buffered instead of sent from the script and just before the flush your callback method is called.
Have a look at using Smarty. It's a templating system for PHP, that is good practice to use, and into which you can plug global output filters.
edit: Paul's reply is better. So it would be
ob_start("my_filter_function");
My original reply was:
That can be achieved with output buffering.
For example:
ob_start();
// Generate all output
echo "all my output comes here."
// Done, filtering now
$contents = ob_get_contents();
ob_end_clean();
echo my_filter_function($contents);

Categories