Save HTML table as image

I have a website that I create that takes the data (person’s name) from the form and puts it in a custom “note” in the table on the next page. I want the user to be able to save his “note” as an image with their configured name on it. Is there any way to save SPECIFIC HTML TABLE as PNG? Maybe take a screenshot of a specific coordinate on a web page?

If there is no way to render hosted PHP content from a form in an HTML canvas?

Any help would be great. Now I'm a little over my head.

+6
source share
5 answers

While you can display HTML using various tools, you cannot be sure that these tools will display HTML the way your browser does.

If your ultimate goal is to generate an image with text on it , then solve this problem instead of trying to get the proposed solution to work.

Take a look at php imagettftext () . It contains example # 1, which creates a small simple image from text that can be saved in any variable ... including a form variable.

Using this example and adding several other PHP GD functions , you can make a decent replica of the table and make sure that it looks exactly the way you want it, and not like html2ps or some other tool.

<?php // Set the content-type header('Content-Type: image/png'); // Create the image $im = imagecreatetruecolor(400, 30); // Create some colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 399, 29, $white); // The text to draw $text = isset($_POST['name']) ? $_POST['name'] : "name"; // Replace path by your own font path $font = 'arial.ttf'; // Add some shadow to the text imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); // Add the text imagettftext($im, 20, 0, 10, 20, $black, $font, $text); // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im); ?> 

Here's the sample output generated by the above script:

enter image description here

Please note that you need to provide arial.ttf in accordance with the instructions at the link above.

If everything does not work, look for errors both on the screen and in the error log of the FIRST web server before monitoring it. Perhaps the PHP GD module is not installed on your web server. If so, you should check with your server administrator to make sure what you need.

+2
source

You can try this client side JS library .

+2
source

workable solution

  • use fpdf to create pdf from html (table)
  • use imagemagick to convert pdf to png
0
source

You can use something like html2ps

0
source

You can also use xvfb . This is the linux package. This means "X-Window Virtual Frame Buffer" (if you are wondering why there was such an esoteric name on earth).

What this will do is load the page, execute any JavaScript and apply various styles from CSS, all in the server frame buffer. Then you can save the image.

Xvfb probably won't be available for most sharing services, however, if that doesn't matter then it will be a viable solution.

0
source

Source: https://habr.com/ru/post/910110/


All Articles