Convert HTML to PDF with HTML_ToPDF class (PHP) - php

In my project, I'm using HTML_ToPDF PHP class, which uses html2ps script to convert from HTML to PDF file.
Recently I upgraded my production server to Debian 5 (Lenny) and, after that, HTML_ToPDF is no longer working:
convert() method from HTML_ToPDF class is returning this error:
Error: there was a problem running the html2ps command. Error code returned: 127. setDebug() for more information.
And, the output from html2ps call is:
/usr/bin/perl: symbol lookup error: /usr/lib/perl5/auto/Compress/Zlib/Zlib.so: undefined symbol: Perl_Tstack_sp_ptr
Any help would be appreciated, thanks!
[Edited]
After some tests, I've found that the problem happens only with HTML containing images ( tags). When html2ps founds the first image it crashes. Converting HTML without images works fine. Any idea? Thanks!

Try this
apt-get install perl=5.8.8-7etch6 perl-base=5.8.8-7etch6 perl-modules=5.8.8-7etch6 debconf-i18n=1.5.11etch2 liblocale-gettext-perl=1.05-1 libtext-iconv-perl=1.4-3 libtext-wrapi18n-perl=0.06-5 libtext-charwidth-perl=0.04-4 html2ps=1.0b5-2 libapache2-mod-perl2=2.0.2-2.4 libcompress-zlib-perl=1.42-2 libfuse-perl=0.07-2+b2 libhtml-parser-perl=3.55-1 libhtml-tree-perl=3.19.01-2 libio-zlib-perl=1.04-1 libwww-perl=5.805-1 perlmagick=7:6.2.4.5.dfsg1-0.14 snmpd=5.2.3-7etch4 xhtml2ps mysql-client=5.0.32-7etch8 mysql-client-5.0=5.0.32-7etch8 libdbd-mysql-perl=3.0008-1 libdbi-perl=1.53-1etch1
http://www.deberias.com

This is an issue with the initial conversion from HTML to PS. Usually due to poorly written HTML.
Can you show some examples of what you are trying to convert?

To be honest I've found wkhtmltopdf to be far better than html2ps, even though it's at a fairly early stage of development.
I wrote a blog about it, but if you don't want to read that basically it uses KHTML/Webkit's rendering engine to render the page which is a bit more sensible than the usual approach of writing a complete HTML renderer.
The distributed binary worked just fine on my Debian server, and frankly the results are excellent.

Related

Accessing Object in PHP

I've some strange issues with some php code.
if ($user->userType=='admin'){
If I use the above command, the php engine just stop interpreting and display the code in plain text on my browser. On the other hand if I use the below method it works:
if ($user['userType']=='admin'){
Again here also:
$_SESSION['currentUser']->id
If I use the above code it just displays the rest of code as plain text:
id); // fail user }else{ $authentication="failed"; $noAuthPresentation="loginForm"; }
Why this is happening? It's a big project and I don't want to change every line where there is an occurrence of ->.
Do I need to change some setting somewhere? I'm using WAMP server with php 5.5.12.
Any help ? Thanks!
You're mixing up types, user is an array, and not an object. Something in your php config is doing something strange to your error display it seems. Right click on the page that has the errors, and view source if possible.
Does login.php contain html and php code by chance?

Wordpress XMLRPC API post (html) error parse error not well formed

I am using Wordpress's XMLRPC API and the IXR_Library php class for WP API. wp.newPost is working normally if I use plain text or just simple text in body/content of the post but when I am posting a autocreated full html/shortcode content, it always gives me this error
Array ( [faultCode] => -32700 [faultString] => parse error. not well formed )
The content I am trying to post is a post content I normally use within WP with shortcodes but I want to post it via php using API since I am trying to automate my blogging.
You can check the body/content I am trying to post below
http://pastebin.com/U94XVZGT
Thats the content thats shown in XML debug mode (probably seems already html encoded)
Below is the full XML call and response returned via debugging, have removed user/pass from it
http://pastebin.com/26Nyx97K
I would really appreciate any possible help, I have already tried php's htmlentities and htmlspecialchar functions to encode my body and even tried str_replace to replace[ ] brackets in thought that they might be causing it
Again, its certainly due to the content code as I tried just a Hello World plain content and it worked ------------------------------------------------------------
A Update, earlier I was getting the html content from a textarea using $_POST, just now I tried to use same content in same variable but used ' ' single quotes instead around the content body and now posting works but I get a error
Warning: strpos(): Empty delimiter in wp-includes/class-wp-xmlrpc-server.php on line 4338
So if I use $body=$_POST['body']; it doesnt works and gives main error but directly using $body=' htmlcontent inside '; works but gives above strpos error
Please try adding the xml package:
sudo apt-get install php-xml
, this has resolved my problem.
TO anyone else having this problem, I fixed it. At end of it seemed the problem was due to wrong character encoding, I needed to Pass UTF8 but the content was being passed in some other char encoding (windows one) making some text and other languages not parseable, I modified my code to make it UTF 8 and now it works
I've changed my password to contain no symbols and it solved the issue.
I'm still not sure what broke everything initially but you might as well try changing it and trying again and see if that works for you.
To anyone else having this problem, it might also relate to WordPress bug #18310.
There is a patch for WordPress available at http://core.trac.wordpress.org/attachment/ticket/18310/18310-3.diff
See the WordPress bug report on their trac install at http://core.trac.wordpress.org/ticket/18310

Scala Lift - Run PHP file from within scala runtime

I'm not entirely sure the wording for the title is correct, but what I'm attempting to do is run and execute PHP files from within the Lift framework.
I'm not after any url queries to a PHP file residing on a server, more interested in somehow getting the PHP runtime working through my Scala/Lift app.
Use case: I have my app packaged into a .war file, I host this via a cloud provider. I upload code snippets to said app which then runs the php file and does whatever necessary.
I've seen various posts regarding Bianca but am hoping to keep this setup light and require only the PHP binary itself and a little code to get it flying.
Thanks in advance, please let me know if you need me to elaborate :)
“Never say never, because limits, like fears, are often just an
illusion.”
― Michael Jordan
What you really need is an open source (GPL), embeddable, full PHP 5 implementation, written entirely in Java!
Caucho's Quercus PHP Java runtime is just that, and it will let you run PHP within a Java app without external libraries or native code.
Below is a Quercus-PHP-in-Java code sample I found in this answer
import javax.script.ScriptEngine;
import com.caucho.quercus.script.QuercusScriptEngineFactory;
QuercusScriptEngineFactory factory = new QuercusScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
String phpCode = "<?php $foo = strlen('abc'); print $foo; return 'yikes'; ?>"; //PHP Code as String
Object o = engine.eval(phpCode);
System.out.println(o);
It should be little effort to convert this code to idiomatic Scala. Obviously, the 'phpCode' variable could be constructed from external PHP file contents etc.
Let us know how you get on ;-)
That's a bit of an odd requirement, but if it's what you need to do, you can use a ProcessBuilder to execute and interact with your PHP script from the command line.

How to read GTFS protocol buffer in PHP?

I have a GTFS protocol buffer message (VehiclePosition.pb), and the corresponding protocol format (gtfs-realtime.proto), I would like to read the message in PHP alone (is that even possible?).
I looked at Google's python tutorial https://developers.google.com/protocol-buffers/docs/pythontutorial and encoding documentation https://developers.google.com/protocol-buffers/docs/encoding and https://github.com/maxious/ACTBus-ui/tree/master/lib/Protobuf-PHP, but I am having a really hard time conceptualizing what is going on. I think I understand that gtfs-realtime.php is a compiled instruction set of the encoding defined in gtfs-realtime.proto (please correct me if I am wrong), but I have no clue how to get it to decode VehiclePosition.pb. Also, what are the dependencies of gtfs-realtime.php (or the python equivalent for that matter)? Is there anything else I have to compile myself or anything that is not a simple php script if all I want to do is read VehiclePosition.pb?
Thanks.
edmonscommerce and Julian are on the right track.
However, I've gone down the same path and I've found that the PHP implementation of Protocol Buffers is cumbersome (especially in the case of NYCT's MTA feed).
Alternative Method (Command Line + JSON):
If you're comfortable with command line tools and JSON, I wrote a standalone tool that converts GTFS-realtime into simple JSON: https://github.com/harrytruong/gtfs_realtime_json
Just download (no install), and run: gtfs_realtime_json <feed_url>
Here's a sample JSON output.
To use this in PHP, just put gtfs_realtime_json in the same directory as your scripts, and run the following:
<?php
$json = exec('./gtfs_realtime_json "http://developer.mbta.com/lib/GTRTFS/Alerts/VehiclePositions.pb"');
$feed = json_decode($json, TRUE);
var_dump($feed);
You can use the official tool: https://developers.google.com/transit/gtfs-realtime/code-samples#php
It was released very recently. I've been using it for a few days and works like a charm.
I would assume something along the lines of this snippet:
<?php
require_once 'DrSlump\Protobuf.php';
use DrSlump\Protobuf;
$data = file_get_contents('data.pb');
$person = new Tutorial\Person($data);
echo $person->getName();
as taken from the man page: http://drslump.github.io/Protobuf-PHP/protobuf-php.3.html
Before that step, I think you need to generate your PHP classes using the CLI tool as described here: http://drslump.github.io/Protobuf-PHP/protoc-gen-php.1.html
so something along the lines of:
protoc-gen-php gtfs-realtime.proto
Sorry Harry Truong, I tried your executable but it returns always NULL.
What I am doing wrong?
Edit: The problem is that I have no permission to execute in my server. Thanks for your executable.

Why is javascript not able to use a javascript variable I declared in a php file?

Hey everybody, this issue has had me stumped for the last week or so, here's the situation:
I've got a site hosted using GoDaddy hosting. The three files used in this issue are index.html , milktruck.js , and xml_http_request.php all hosted in the same directory.
The index.html file makes reference to the milktruck.js file with the following code:
<script type="text/javascript" src="milktruck.js"></script>
The milktruck.js file automatically fires when the site is opened. The xml_http_request.php has not fired at this point.
On line 79 out of 2000 I'm passing the variable "simple" to a function within the milktruck.js file with:
placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);
"simple" was never initialized within the milktruck.js file. Instead I've included the following line of code in the xml_http_request.php file:
echo "<script> var simple = 'string o text'; </script>";
At this point I have not made any reference whatsoever to the xml_http_request.php file within the milktruck.js file. I don't reference that file until line 661 of the milktruck.js file with the following line of code:
xmlhttp.open('GET',"xml_http_request.php?pid="+pid+"&unLoader=true", false);
Everything compiles (I'm assuming because my game runs) , however the placem function doesn't run properly because the string 'string o text' never shows up.
If I was to comment out the line of code within the php file initializing "simple" and include the following line of code just before I call the function placem, everything works fine and the text shows up:
var simple = 'string o text';
Where do you think the problem is here? Do I need to call the php file before I try using the "simple" variable in the javascript file? How would I do that? Or is there something wrong with my code?
So, we meet again!
Buried in the question comments is the link to the actual Javascript file. It's 2,200 lines, 73kb, and poorly formatted. It's also derived from a demo for the Google Earth API.
As noted in both the comments here and in previous questions, you may be suffering from a fundamental misunderstanding about how PHP works, and how PHP interacts with Javascript.
Let's take a look at lines 62-67 of milktruck.js:
//experiment with php and javascript interaction
//'<?php $simpleString = "i hope this works"; ?>'
//var simple = "<?php echo $simpleString; ?>";
The reason this never worked is because files with the .js extension are not processed by PHP without doing some bizarre configuration changes on your server. Being on shared hosting, you won't be able to do that. Instead, you can rename the file with the .php extension. This will allow PHP to process the file, and allow the commands you entered to actually work.
You will need to make one more change to the file. At the very top, the very very top, before anything else, you will need the following line:
<?php header('Content-Type: text/javascript'); ?>
This command will tell the browser that the file being returned is Javascript. This is needed because PHP normally outputs HTML, not Javascript. Some browsers will not recognize the script if it isn't identified as Javascript.
Now that we've got that out of the way...
Instead I've included the following line of code in the xml_http_request.php file: <a script tag>
This is very unlikely to work. If it does work, it's probably by accident. We're not dealing with a normal ajax library here. We're dealing with some wacky thing created by the Google Earth folks a very, very long time ago.
Except for one or two in that entire monolithic chunk of code, there are no ajax requests that actually process the result. This means that it's unlikely that the script tag could be processed. Further, the one or two that do process the result actually treat it as XML and return a document. It's very unlikely that the script tag is processed there either.
This is going to explain why the variable never shows up reliably in Javascript.
If you need to return executable code from your ajax calls, and do so reliably, you'll want to adopt a mature, well-tested Javascript library like jQuery. Don't worry, you can mix and match the existing code and jQuery if you really wanted to. There's an API call just to load additional scripts. If you just wanted to return data, that's what JSON is for. You can have PHP code emit JSON and have jQuery fetch it. That's a heck of a lot faster, easier, and more convenient than your current unfortunate mess.
Oh, and get Firebug or use Chrome / Safari's dev tools, they will save you a great deal of Javascript pain.
However...
I'm going to be very frank here. This is bad code. This is horrible code. It's poorly formatted, the commenting is a joke, and there are roughly one point seven billion global variables. The code scares me. It scares me deeply. I would be hesitant to touch it with a ten foot pole.
I would not wish maintenance of this code on my worst enemy, and here you are, trying to do something odd with it.
I heartily encourage you to hone your skills on a codebase that is less archaic and obtuse than this one before returning to this project. Save your sanity, get out while you still can!
perhaps init your values like this:
window.simple = 'blah blah blah'
then pass window.simple
You could try the debugger to see what is going on, eg. FireBug

Categories