I am having some trouble with multidimensional array and its value.
What i am looking for is , from my query I am searching teachers name in the array. And after that i want to create a pdf using dompdf. The problem is with looping. I am not able to create a proper loop which will work the way I want it to work. My sample query is
$q11 = "select id from teachers order by teacher ";
$r11 = mysql_query($q11) or die(mysql_error());
while($rows11 = mysql_fetch_array($r11)){
$teacher = $rows11['id'];
$dompdf->"It will start working";
}
Now i know , this code is confusing, but what i want is, it should create dompdf for every teacher in one single pdf file. Like from the query it should fetch teachers, and for each teacher it should create a dompdf page. Currently it is making just one page according to the last value that my query has search.
Please help. It is kinda urgent.
Your loop is working fine. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
EDIT
I've never used dompdf. A quick look into the docs let me think you create something like a HTML markup which then is converted into PDF, did I get this right?
Example code
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
while ( $row = $dbResult->fetch_assoc() ) {
$html .= '<div class="teacherPage">'
. $row['name'] // your teacher document goes here
'</div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
If you wonder about the unusual syntax $var = <<<HTML \r\nHTML, that's a heredoc. It's just more comfortable to use heredocs when you have a lot of alien inline code, this can have variables {$varname} and you don't need to worry about quotes. All you need to make sure, is that heredoc closer HTML is in a new line and not indented.
EDIT2
Still not too sure, which library you are using. I find this extension looking pretty good and it's called dompdf, just like you said in your question.
Your latest comment indicates you did not solve your problem so far, so I decided to add some more information to get you to the target.
Disclaimer: I am not going to write functional code and I will not test this, but the following hints will push you into the right direction to get your stuff done.
dompdf is able to read CSS2 and CSS3 properties of your input document.
Each cycle in the while loop above represents one teacher whith each of them getting a own page in the output document.
I put the page into a div container with the class teacherPage. You can fill this container with all the information you want to have displayed for a teacher.
Now all we need to do, is to tell dompdf each teacherPage is a new page. This can be done using #page markup shipped with CSS3
I added an empty css container <style type="text/css"></style> to the example document above, that's where the page styling should go to.
The example CSS
#page teacher {
size: A4 portrait;
margin: 2cm;
}
.teacherPage {
page: teacher;
page-break-after: always;
}
With #page you can define a named page teacher, which can have properties valid for the whole page container.
page-break-after: always will begin a new page after each container
Hope this helps, have fun trying :)
Apparently, no overflow problems and the results are taking more than the permitted margin of the sheet. What you can do is to separate data from the parent in another table and put between them:
<div style="page-break-before: always;"></div>
Related
In wordpress there is a plugin that assigns a header graphic for each page. You call that header graphic by placing this code in your header.php file:
<?php if(function_exists('show_media_header')){ show_media_header();} ?>
This basically calls the image assigned and places it as an IMG in HTML.
I would like to have it called as a background image with CSS but don't know how. For example:
.header-graphic{ background:url("show_media_header();"); }
I know that will obviously not work but that should explain what I'm trying to do.
Any help would be great.
Thanks!
Depending on the scope of show_media_header() and that it actually returns the path to an image you could write the following:
.header-graphic{ background:url("<?php echo show_media_header(); ?>"); }
However this is of course under the assumption that your css is in the php-file, which wouldn't be recommended. You should look at using SASS or LESS instead.
It's generally a bad idea to serve static files (like CSS) dynamically, since it can't be cached effectively. So inserting the result of show_media_header() directly into your CSS is a no-go.
However, there is an alternative: Insert just that style into the HTML like so:
<h3 style='background-image: url("<?= show_media_header(); ?>");'>
Foo
</h3>
Which can then be further modified by CSS that is in a statically-served and unchanging file - for example:
h3 {
background-position: left 3px top 3px;
}
This of course assumes the function returns just the image URL; I've not used Wordpress personally.
Based on another comment, apparently this function generates a complete <img> tag (ugh!) so you might instead have to do something like this:
<h3>
<?= show_media_header(); ?>
Foo
</h3>
And style it as appropriate like so:
h3 img {
margin: 3px 0 0 3px;
}
I'm gonna post it down here because no one is considering your statement:
"and places it as an IMG in HTML"
You may have to edit you plugin output. Since show_media_header(); echo a value, the function itself is creating a <img> element. Look for the plugin file, search for the function and, either create another one, duplicating the original, something like show_media_header_bg where you manipulate the element, or change the original.
How about if you use descendant CSS selectors as such:
#page #header {
background-image: url("image.jpg");
}
#another-page #header {
background-image: url("another-image.jpg");
}
and so assign each page to its background image.
Here, I'm assuming you can grab into each page by an id (here called "page" and "another-page", and that your header template has an id of header. It would help to see some HTML to see how best to exactly achieve this via CSS.
Got it to work!
Dug around in the plugin PHP file and found this:
function get_media_header_url() {
global $post;
$post_id = $post->ID;
So I did this:
.header-graphic-background{ background:url("images/<?php echo get_media_header_url() ?>"); }
Works great!
You guys absolutely pointed me in the right direction. THANKS!!!
First off, I'm brand new to PHP so I'm sorry if this is a stupid question, second of all sorry if this title is incorrect.
Now, what I'm trying to do is create an overlay for a game that I play. My code for the overlay works perfectly, and now I'm working on my HTML file which gets its information from a website and outputs it. The code on the website looks like this:
<span id="example1">Information I want</span>
<span id="example2">More Info I want</span>
...
<span id="example3">And some more</span>
Now what I want to do is create a PHP script which goes in and finds elements by their names and gives me the information in those span tags. Here's what I've tried so far, it's not working however (no surprise):
//Some HTML here
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->Load('www.website.com');
echo "Example1: " . $doc->getElementById('example1') . "\n";
?>
//More HTML
To be honest, I have no clue what I'm doing. If anyone could show me an example of how to do this properly, or to point me in the right direction I would appreciate it.
The text between open and close tags is a Text Node.
Just write $doc->getElementById('example1')->nodeValue
Your code seems along the right lines, but you're missing a few things.
First of all, your load call is literally looking for a file named "www.website.com". If it's a remote file, you must include the http:// prefix.
Then, you are attempting to echo out the node itself, whereas you want its value (ie. its contents).
Try $doc->getElementById("example1")->nodeValue instead.
That should do it. You may want to add libxml_use_internal_errors(true); so that any errors in the source file won't destroy your page with PHP errors. Also, I would suggest using loadHTMLFile instead of load, as this will be more lenient towards malformed documents.
you can use getElementById:
$a = $doc->getElementById("example1");
var_dump($a); so you will see what you want to echo or put, or something.
You can also make all the names i HTML as example[] end then foreach the example array, so you can get element by id from example array with just one row of code
I am building a website that displays recipes. Each recipe appears as part of a blog entry, and will have a link at the bottom to Print this recipe.
What I want to happen is a click on the link opens a new window and fills it with a print-friendly-styled version of the recipe, which is already inside its own <div class="recipe">.
Can I do this with JS/jQuery alone, or do I need to process from the server side? Any ideas how to do this?
EDIT: What would be ideal would be to generate a PDF on the fly, but in lieu of that I'd like a new window, containing only the recipe, for the visitor to print out or save, as they see fit. Print-styles are nice, but most people don't know they exist and can't be bothered to check and see by printing out a page that doesn't look print ready.
There is no need to load a different stylesheet and the only javascript you will need is for triggering the printing dialog.
With CSS alone you can add rules that are only used when printing, you can either use media queries
<style type="text/css">
#media print{
//css printing rules
}
</style>
or use the link tag:
<link rel="stylesheet" media="print" href="styles.css" type="text/css" />
UPDATE: If you want to update the stylesheet on the fly without openning a new window i suggest you check out this Nettuts article or a simpler solution:
$("#css-switch").click(function() {
$("link[rel=stylesheet]").attr({href : "red.css"});
});
You can do this simply with CSS alone if you have it load the exact same page but with a different stylesheet.
Yes, you can(Hello Mr. Obama).
Most browsers allow you to pass in a data: format string, like
window.open('data:text/html;charset=utf-8,text%20to%20show');
which would open a new window / tab (that is browser config dependend) with the Content "text to show". You can pass in HTML code in the same manner, probably escaped.
var print = $('<div>', {
id: 'foobar',
html: 'Hello world',
css: {
backgroundColor: '#ff0000',
color: '#ffffff',
width: '200px',
height: '200px'
}
}),
opener = $('<div>').append(print);
window.open('data:text/html;charset=utf-8,' + opener.html());
Demo: http://www.jsfiddle.net/4yUqL/73/
You'll probably need to do a request to the server on the print page and fill in the form fields with the data.
If i was you, i'd go for the pdf creating solution. It is fairly simple to create pdf's on the fly in php, and it will almost certainly give a better user experience to your main audience.
If you want to skip that excersise, i'd do it the following way:
1: fetch the data you need as JSON on the original non-printerfriendly page, and use clientside templating to build the ui. Store the JSON for use on the printerfriendly page
2: when you open the new window, use the exact same method, but use another template optimized for printing.
A client of mine has a food blog hosted on WordPress. Each post entry contains some text and a div called "recipes" with some more text inside it. They would like to add to this div a link that generates a PDF of the recipe, dynamically, for saving or printing, as the user sees fit.
I have seen quite a few Wordpress plugins that offer the conversion of entire posts to PDF but not anything that's customizable enough to select a given portion of a post, the way we'd like to.
Any suggestions on how to do this? I'm comfortable with PHP, Javascript, CSS but am new to the various PDF libraries.
Take a look at dompdf It's pretty easy to work with :) This is from the documentation:
<?php
require_once("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("sample.pdf");
If you need more control than dompdf you could always use PHP's XML/XSL methods to convert the HTML to XSL:FO and use FOP on the commandline to generate the PDF. It's a little long-winded but you get complete control of the output styling/structure, the ability to "lock" the PDF, provide metadata, etc.
I am in the process of generating a dynamic pdf file, which contains data of around 10,000 users, in general the app is developed using MySQL and PHP. The dynamic content is so heavy that, I found it difficult to process with fpdf() class. So I converted my output PHP page as a HTML file using ob_get_clean(). Now the html file is generated successfully and also the pdf file. But i want to leave a page break after each user's data, that is every user's data must start in a fresh page. I couldn't use any HTML tags because, in the dynamically generated HTML file, everything is out of the <html> and </html> tags. Please help me so that some how i make a page break in the pdf file after every user's data... Thanks in advance :)
html2pdf supports page tag:
protected function _tag_open_PAGE($param) {}
at line 2229. You can see there what attributes are supported. For example following creates one page in landscape and one in portrait mode:
<page orientation="l">
... some content ...
</page>
<page orientation="p">
... some content ...
</page>
Basing on macdabby's work (which doesn't work). But thanks to him, the idea is correct.
Html2Pdf v4.03
For example we want to parse a tag DIV:
html2pdf.class.php line 2948:
protected function _tag_close_DIV($param, $other='div')
{
if ($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage(null, '', null, $this->_defaultTop);
$this->parsingCss->setPosition();
...
}
parsingCss.class.php Line 114:
//add a new style declaration
public function initStyle()
{
...
$this->value['page-break-after'] = null;
}
Line 1024 add a new handler to the switch case:
case 'page-break-after':
$this->value[$nom] = $val;
break;
And then for it to work, your html content should contain the break element
<div style="page-break-after:always; clear:both"></div>
Watch out for case sensitive style, not sure if the plugin handle it
i just figured this out after having the same problem. the parser that they use DOES support the page-break-after tag, but the html2pdf does not work.
i think i have it working by doing the following modifications to html2pdf.class:
around line 4174, the first thing inside:
protected function _tag_close_P($param){
should be:
if($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage();
around line 2961, the first thing inside:
protected function _tag_close_DIV($param, $other='div'){
should be:
if($this->parsingCss->value['page-break-after'] == "always")
$this->_setNewPage();
You possibly want to use some css, eg:
h1 {page-break-before:always}