Open page as pdf with DOMPDF - html

I am currently working on a semi dynamic page where I use some GET functionality to personalize it. I also echo the date a couple of places. At the bottom of this page, I would like to have a button that gives the visitor the option to download/open this page as PDF. Without the header. I have integrated DOMPDF, but I simply cant get it to work properly and need some help. I have tried a couple of things found here on Stackoverflow, with no success.
In basic, I need the whole page printed in the PDF, but it should not open when page is loaded. But triggered by the button. And then without the header (one spesific div). Is this possible?
<?php
require_once("dompdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>This is a test for '.
'<?php echo htmlentities(substr(urldecode($_SERVER["QUERY_STRING"]), 1)); ?> </p>'.
'<p>Thank you for reading.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("the_inquiry.pdf");
return true;
?>
<html>
<body>
<div class="shouldnotPrintToPDF">
Content.
</div>
<div class="shouldPrintToPDF">
Sensitive content.
Open or save as PDF
</div>
</body>
</html>
This is basically our one page presentation. And contains a lot of text, so I will not present all of that here. But in this way, I have to write the page twice, in $html = as well as inside the actual tag. And the PDF save/open option pops up right from the start, which it should not. I also wish to append the echo htmlentities part to the actual pdf-name.. is this possible? The PDF opens and contains what put into the $html = just fine. But not triggered by the link.
Update:
When i do exactly what you perform here, I get an error "The requested URL /inquiry.php&pdf=1 was not found on this server." I have the page I am trying to print in pdf on root level, but the DOMPDF is in /dompdf.. I dont know if that has anything to do with it?
Update:
When i edited the link, i get all this information up in a new page, like below.
[_parse_properties(margin:=1.2cm)(empty)_parse_properties]
[_parse_sections[section[_parse_properties(display:=-dompdf-page)
(counter-reset:=page)(empty)_parse_properties]#html#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]
_parse_sections][_parse_sections[section[_parse_properties(display:=block)
(empty)_parse_properties]#div##map##dt##isindex#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]
_parse_sections][_parse_sections[section[_parse_properties(page-break
-before:=avoid)(display:=block)(counter-increment:=page)
(empty)_parse_properties]#body#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin:=1em
0)(empty)_parse_properties]#p##dl##multicol#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin-left:=40px)
(empty)_parse_properties]#dd#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin:=1em
40px)(empty)_parse_properties]#blockquote#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(font-style:=italic)
(empty)_parse_properties]#address#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(text-align:=center)
(empty)_parse_properties]#center#section]
Do you have any idea what it might be caused by?
Breakthrough:
When I activated DOMPDF_DPI it actually opens as PDF, but now all of the text comes on first line of the second page of the PDF. Like, all the text comes out on top of each other. Also, when it opens the PDF, the ?&pdf=1 are included in the htmlentities query string, which looks very messy since it is supposed to be a personalized page as well as the PDF.

You can set dompdf to parse CSS #media queries for standard media types (screen, print, voice, etc.). By default dompdf parses the "screen" media type styles, but you can change this in the configuration file. See the DOMPDF_DEFAULT_MEDIA_TYPE configuration setting. Then you just need to pass the original URL querystring with an appended variable telling the page to render to PDF. If your original URL is something like the_inquiry.php?name=Joe then your PDF URL could be the_inquiry.php?name=Joe&pdf=1.
Your code would then look similar to the following:
<?php
ob_start();
?>
<html>
<head>
<style>
#media print {
.shouldnotPrintToPDF, .pdflink { display: none; }
}
</style>
</head>
<body>
<div class="shouldnotPrintToPDF">
Content.
</div>
<div class="shouldPrintToPDF">
Sensitive content.
Open or save as PDF
</div>
</body>
</html>
<?php
if ( isset( $_GET['pdf'] ) ) {
require_once 'dompdf/dompdf_config.inc.php';
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("the_inquiry.pdf");
}
?>

Related

hyperlink text not visible on webpage but link is there

I am not sure why my text for the link is not visible on the page but, when I take my mouse to the area where I was expecting a link text then I am able to click that
blank area" and able to download correct file!
Here is the code structure:
<html>
<head>
some heading/styling link etc.
<body>
some code related to creating dropdown and populating it (form used)
<?php
if submit button is clicked then bunch of variables are populated and
user receives an email with result file. Ideally, I would like to be able
to display a result file link inside this php code.However, I don't know
if php allows to create a hyperlink. So, I was testing to create a link
outside php code. The php code does some python query.
exec("/path/to/python script that does query $var1 $var2 $var3 2>&1",
$output)
?>
<a href="/<?php echo $output[8] ?>"Link to query result</a>
</body>
</html>
So, the text "Link to query result" is nowhere visible but the link exists. What is happening here?
Anchor's opening tag misses '>'
Change
<a href="/<?php echo $output[8] ?>"Link to query result</a>
to
Link to query result

Pass auto direct page after login to the webpage

I have successful login to a webpage by the script below but the webpage stuck at the redirect page. I has no idea how to pass through it. Below is my code
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
my $URL="http://www.redirect.com";
my $UA = LWP::UserAgent->new();
$UA->ssl_opts( verify_hostnames => 0 );
my $req =HTTP::Request::Common::POST("$URL",
Content_type=>'form-data',
Content =>[
'username'=>'name',
'password'=>'pass',
]
);
my $resp=$UA->request($req);
if ($resp->is_success) {
my $res2 = $UA->post($resp->base, []);
open(OUTFILE1, ">html1.txt");
print OUTFILE1 $res2->decoded_content;
if ($res2->is_success) {
if( ($resp->code() >= 200) && ($resp->code() <400) ) {
open(OUTFILE, ">html.txt");
binmode(OUTFILE, ":utf8");
print OUTFILE $resp->decoded_content;
}else{
print "Error: ". $resp->status_line. "\n";
}
}
}
This is the part of the output file i get
<HTML>
<HEAD>
<TITLE>
</TITLE>
</HEAD>
<BODY onLoad="document.AUTOSUBMIT.submit();">This page is used to hold your data while you are being authorized for your request.<BR>
<BR>You will be forwarded to continue the authorization process. If this does not happen automatically, please click the Continue button below.
<FORM NAME="AUTOSUBMIT" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded" <INPUT TYPE="SUBMIT" VALUE="Continue"></FORM>
</BODY>
</HTML>
How to pass through this auto direct page to reach the website i want? Have modified according to the answer but nothing was output.
Edit 8/7/2017
I try the way simbabque suggest and debug by print out the $res2 in html1.txt. The output look like below
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved here.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at www Port 80</address>
</body></html>
I have no idea what this mean. Can anyone help?
The interesting part about this is probably the headers. A normal website would issue a redirect status code, like a 302 Found. But that's clearly not the case here, or they wouldn't need this rather strange HTML thing.
The HTML has built-in JavaScript execution.
<BODY onLoad="document.AUTOSUBMIT.submit();">
It tells the browser to submit the form directly when the page has loaded. Your problem is that LWP::UserAgent cannot do that, because it doesn't have JS support.
But since this always happens, it's trivial to code around it. All you need to do is submit that form every time you log in.
my $res = $ua->request($req);
if ($res->is_success) {
my $res2 = $ua->post($res->base, []);
if ($res2->is_success) {
...
}
}
The form has no parameters. The only <input> element there is is the submit button, and since that doesn't have a name attribute it does not show up as a parameter. The URL is likely the same as the one you submitted originally, but it might have done a real redirect already, so it's better to use the base attribute of the response object.
I do wonder why they make this process so weird. It certainly doesn't authorize anything. It might set additional cookies, like one of those marketing redirect thingies, but that's not visible from what you've showed. And it does not stop automation either.

Wordpress - Having head, body into WYSIWYG editor

I created my event pages with a different website. That website generated an html page for me(with Javascript, CSS hosted at their end) each time I created an event. I would like to embed the event pages into my website. In order to achieve, I tried to create a blank page template like below:
<?php
/**
* Template Name: Blank
*
*/
if (have_posts()) {
while (have_posts()) { the_post();
the_content();
}
}
?>
Then pasted the content (including head and body) into WYSIWYG editor. However, WordPress pushes everything into body. How can I create a pure blank page template and have the ability to edit everything with WYSIWYG?
Not quite sure if this is the best way to solve your problem. If I understand correctly you want to include events from website X in a page on website Y?
Assuming you set up events as a custom post type, you could use an 'event' RSS feed on website X: http://www.website-x/feed/?post_type=event
Then, on website Y reed the feed using:
$content = file_get_contents('http://www.wesite-x/feed/?post_type=event');
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry)
{
$title = $entry->title;
//ETC...
}
It's not wysiwyg but it well get you all the content you need, and you can do whatever you want with it.
Hope this helps.

JpGraph Error: HTTP headers have already been sent

The problem is that the JpGraph is not displayed correctly on my web-page. The strange thing is that if I run the above code in isolation, then it works. But if I insert it in my main code, it fails producing the above-shown message.
P.S. I'm using 'ob_start();', but it does not solve the problem.
// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");
// A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);
// Display the Gantt chart
$graph->Stroke();
?>
</div>
JpGraph Error: HTTP headers have already been sent.
Caused by output from file index.php at line 85.
Explanation:
HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).
Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser.
For example it is a common mistake to leave a blank line before the opening "<?php".
JpGraphs can't exist in files with html. They have to be in a pure php file. To get around this, I created a seperate file that generates the graph, and made the whole thing a function. At the end, change
$graph->Stroke();
to
$graph->Stroke(".<filepaht>.jpg");
Then, in your index.php page, reference the image file.
So, what it looks like you need is,
createjpgraph.php:
<?php
function GenGraph (<input variables>) {
// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");
// A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);
// Display the Gantt chart
$graph->Stroke("./foler/file.jpg");
}
?>
index.php:
...
<div>
...
<?php
include 'createjpgraph.php';
GenerateGraph(<variables>);
?>
<img src=\"./folder/file.jpg\" />
</div>
Hope this works for you.
You can even do it in the same html document - first writing the graph to a file, then displaying it ...
//Code generating your graph here ...
// Finally output the image to a file
$graph->Stroke("/var/www/html/tmp/out.jpg");
//end of php code
?>
<!-- now include the newly generated image in the HTML -->
<img src="/tmp/out.jpg">
</body>
</html>
It is seem that the jpeg file that is being created by the function cannot be over written in my case...
to do overwrite it..
I changed my JPGraph file gd_image.inc.php .. you have to comment out the line
that says JpGraphError::RaiseL(25111,$aStrokeFileName)

GET request in AJAX-retrieved HTML

There is some HTML code which appears on the page as the result of AJAX request/response. This HTML contains GET requests to another php script. Links containing this requests are not working while "real" HTML (added on the page manually) works normally.
When I open the page source with Ctrl+U I see empty div's (but they actually have HTML retrieved via AJAX) and the full-fledged HTML code added manually. Can't figure out how can I make the AJAX-retrieved links working.
This is how I try to form the HTML code which must be added to the page as the result of AJAX response.
<?php
//some selections from the database...
//all subsequent varibles are the members of $row = mysql_fetch_array($query) array;
$html_to_be_inactive =
'<li id="productID_' . $productID . '">
<a href=work.php?action=deleteFromBasket&productID=' . $productID . ' onClick="return false;">
<img src="images/delete.png" id="deleteProductID_' . $productID . '">
</a> ' . $productName . '(' . $totalItems . ' items) - $' . ($totalItems * $productPrice) . '</li>';
echo $html_to_be_inactive;
?>
And that is it, result of 'echo' appears on the page after successful AJAX request as the simple HTML, and it does nothing when I click the link.
I do not perform DOM 'append' methods as I'm not familiar with DOM yet. I can see all the elements mentioned in $thml_to_be_inactive but the link seems to be really inactive. Also this HTML is absent in the "Ctrl+U" output, I can see it only when I select "Inspect element" in the browser.
The HTML which was added manually operates the same items as usual, but I need AJAX-retrieved HTML to be working as well.
I suspect I need to perform some 'append' method via JavaScript or jQuery, but I don't know how to do it in a proper way.
Thanks.
You need to put your href in quotes.
onClick="return false;" will override the default action (go to href) of your link. You need to bind an other event to the link, or just remove `return false'.
Your links use relative paths.
You're probably inserting the HTML into a page in a different directory, breaking the relative paths.
If so, you need to switch to absolute paths.