Make pdf data unselectable via php, converting everything to on image - php

So I am useing DomPDF to create my document. It draws everything on canvas. I need the resoult data on one page to become one image on this page. Make it unselectable, so it will look like it was scanned rather then created.

Depending on your solution you may want to look at this package spatie/browsershot. It is for Laravel, but you can see it making use of Puppeteer to take html and create either a PDF or an image.
From what I remember DomPDF is using an older rendering engine, where as puppeteer makes use of Google Chromes engine, so can render newer markup. So may even get a better looking result!

Related

How to conver HTML + Javascript to PDF using PHP

I'm trying to convert a HTML page which contains Javascript (that formats certain information after the document is loaded) to PDF, using PHP prefarably.
The javascript code will look something like this:
$(document).ready(function() {
formatProfile();
});
This will format the raw data and replace it to the specific DIV.
However, when I do a render and try to convert it to PDF the DIV remains empty, as if the javascript component is not rendered at all.
I've tried using mpdf, wkhtmltopdf, dompdf and many more but none work as intended. Is there any library that will work or is there any workaround (assuming I still prefer to use the javascript to process and output the results to DIV)?
PhantomJS is probably your best bet here. You'll have to install the software on your server, but it gives a nice implementation of webkit in a headless setup so that you can completely render a page, javascript included and then capture the screen output to a PDF.
EDIT:
You might look at this PHP project which wraps the PhantomJS application in a PHP script for easier web screen capture from within PHP.
You can try this http://www.tcpdf.org/ . Site providing some good examples and also you can design page layout as your own styles. I think this will solve your issues.
PDFreactor 6 can convert HTML to PDF and process the JavaScript. It also provides a PHP wrapper.

Render Html + CSS page as PDF without using API and libraries but using header type

I want to display Html page generated using PHP and CSS directly as a pdf.
I have searched a lot for this but endup with lot of libraries to do these task. But I don't want to use any libraries and just want to render it as PDF by defining header tag.
I am not sure but, have little in memory it is possible without using any API, library which might be wrong so want to confirm before proceeding to another solution.
Hope there might be the simple way to do this, as this is the most required feature for most of product.
First of all, I'd recommend using a library for this because although it "might be wrong", it's probably more correct than what you or I would come up with. Second, (most of) this isn't done using headers ;)
If you REALLY want to do it yourself, the native PDF functions in PHP are found here, which libraries like FPDF (my personal favorite) use directly in their implementations.
To print a page to PDF, you'll have to render it first (create the HTML DOM and apply the CSS) which PHP does not do. You'll have to render the page first with an HTML engine like WebKit, and then return to PHP for the print operation.
Again, this should probably be done using a library, saving you headaches and debugging. Of course, this question has been asked before so there's plenty of info available.
But if you want to have a go yourself, that's the path you must walk (or crawl). Me, I'd take the bus.
UPDATE
To answer your question in the comments:
<?php header("Content-type: application/pdf;charset=UTF-8"); ?>
Putting this on the top of the page render the pdf but it's not
working generated in this way. So, can you suggest any thing for this.
That header is for an HTTP request. When the request is returned from the server, it sends a header suggesting what type of content is being returned (JSON, an image, a pdf, whatever).
In other words, that header doesn't actually make a PDF, it just tells the browser to expect a PDF. After the header, the server then sends something (hopefully, a PDF).
That's why I said your original question isn't done using headers.

Convert HTML to Image in PHP without shell

I want the option of converting HTML to image and showing the result to the user. I would be creating an $html variable with PHP, and instead of displaying using echo $html, I want to display it as an image so the user can save the file if they needed to.
I was hoping there would something as simple as $image = convertHTML2Image($html); :p if that exists?!
Thanks!!
As #Pekka says, the job of turning HTML code into an image is the job of a full-blown web browser.
If you want to do this sort of thing, you therefore need to have a script that does the following:
Opens the page in a browser.
Captures the rendered page from the browser as a graphic.
Outputs that graphic to your user.
Traditionally, this would have been a tough task, because web browsers are typically driven by the user and not easy to automate in this way.
Fortunately, there is now a solution, in the form of PhantomJS.
PhantomJS is a headless browser, designed for exactly this kind of thing -- automated tasks that require a full-blown rendering engine.
It's basically a full browser, but without the user interface. It renders the page content exactly as another browser would (it's based on Webkit, so results are similar to Chrome), and it can be controlled by a script.
As it says on the PhantomJS homepage, one of its target use-cases is for taking screenshots or thumbnail images of websites.
(another good use for it is automated testing of your site, where it is also a great tool)
Hope that helps.
This is not possible in pure PHP.
What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.
You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).
It is possible to convert html to image. However, first you must convert to PDF. see link
You may have a look at dompdf which is a php framework to convert a html file to a pdf.
use WKHTMLTOPDF. works like a charm. it converts to any page to PDF ..
a jpeg can be obtained by performing later operation.
http://code.google.com/p/wkhtmltopdf/

Programmatically create image from web-page or a single DIV

Is there any way to programmatically create (client or server side (PHP)) a image from a specific DIV or a complete (web) page? I'm currently creating a web-site for free coupons and the idea is when the end-user clicks on the "Print" button, the app opens a new tab/window with all the selected coupons as a single image (JPG, PNG or etc..) in A4 format ready for printing. Each coupons has it's own data (Article name, price, description etc..) so I need it to be done programmatically over a coupon-template I designed.
I do not ask you to write code for me, just to suggest a solution I could use/develop. If not already exist, I will upload/publish it for free :)
Update: I did it with the PHP GD library :) Still not satisfied with the idea to use Images instead of PDF, because each printing results with different Coupon sizes (images) on different PC's. That's why PDF may would be a better solution. You can see/test it on demo.svikuponi.ba - Just select a few Coupons and click the PRINTAJ button above.
You cannot create image from div for sure but yes you can create dynamic images in php using its gd library.
Following links will help:
http://php.net/manual/en/function.imagecreate.php
http://phptutorial.info/learn/create_images/
Here is a great way for you to create images on the client side: http://smus.com/screen-capture-for-chrome-os
You can take this and create a web app that will work nicely on webkit (for other browsers - I'll look at JS polyfills).
Did anyone mention html2canvas and/or jsfeedback ?
It create a page screenshot completely in javascript, then you can send to the server via ajax..
Obviously, CSS support lack some things.
In php, there is many image related functions like imagettftext() in GD library
for details, check this out http://php.net/manual/en/book.image.php
if GD is not enough, you can try imagick as well
for the template, you can try creating a true color handle in php from your file(image) and add the text part or something else with all kinds of effects and bar codes etc.
but in your case, i would suggest dynamic PDF creation since it would better with formatting instead of plain image, the pdf lib :
http://www.fpdf.org/
you could easily have a background image of your token/voucher and overlay the text using some php variables.
i believe it is possible to create a unique bar-code with php imaging too.
It is possible to get a screenshot from a webpage, but this is quite a hassle. You need to start a webbrowser to render the page and get a screenshot from that.
You are probably better of by parsing some specification and feeding it to a couple of GD or Imagick functions. This is less versatile, but easier to manage.

Generate PDF from HTML PHP

I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and CSS to these libraries?
mpdf is another option that you could try.
EDIT :
Found another solution for it, TCPDF is a FLOSS PHP class for generating PDF documents. Looks more dominating library.
"PRINCEXML" is a good library (not completely free now).
Others:
If your meaning is to create a PDF file from PHP, pdflib will help you (as some other suggested).
Else, if you want to convert an HTML page in PDF via PHP, you'll find
a little trouble outta here.. For three years I have been trying to do it as best as I
can.
So, the options I know are:
HTML2PS: same of DOMPDF, but this one convert first in .ps
(Ghostscript), then, in whatever format you need (PDF, JPEG, PNG). For
me it is a little better than dompdf, but I have the same speed problem.. Oh,
it has better compatibility with CSS.
Those two are PHP classes, but if you can install some software on the
server, and access it through passthru() or system(), have a look at
these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and
powerful... It seem like it is the best one (atm) for converting HTML pages to PDF on the fly, taking only two seconds for a three pages XHTML document
with CSS 2. It is a recent project. Anyway, the Google Code page is often
updated.
htmldoc: this one is a tank, it really never stops orcrashes... The project
seems to have died in 2007, but anyway if you don't need CSS compatibility
this can be nice for you.
** Thumbs Up For Strae.
If I understand your needs correctly I don't think any PHP-PDF class would do that.
Mostly you could insert only text and images to a PDF file, so if you would want something that looks like an HTML element you would need to insert it as an image.
Usually just putting HTML doesn't mean all your elements would stay intact in the PDF . (Different world, after all)
http://www.fpdf.org/ is the site having a great HTML-to-PDF class which work well. I am using it, but you have to first study its functionality and then start.

Categories