HTML file converts it to image file locally

I have a file a.htmlthat contains a table, and I want to convert and save it locally as an image.

Is it possible?

To save it as an image, I tried this:

ImageIO.write(image, "png", new File("image.png"));

a library for converting html to image , but it is for the html online page.

+4
source share
3 answers

Here is the program using swingsprovided by java.

public class MyClass {

    public static void main(String[] args) throws IOException {
        URL url = MyClass.class.getResource("myhtml.html");
        Dimension size = new Dimension(200, 200);
        Image img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JEditorPane pane = new JEditorPane(url) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
            }
        };
        frame.setSize(size);
        frame.add(pane);
        frame.setVisible(true);
        Graphics g = img.getGraphics();
        pane.paintAll(g);
        ImageIO.write((RenderedImage) img, "jpg", new FileOutputStream("myimg.jpg"));
        frame.setVisible(false);
        frame.dispose();
    }
}

But it will show a flash (uncleared frame for a second).
here is the example i used:
myhtml.html:

<table border="1">
    <tr><td>one</td><td>two</td><td>three</td></tr>
    <tr><td>four</td><td>five</td><td>six</td></tr>
    <tr><td>seven</td><td>eight</td><td>nine</td></tr>
</table>

enter image description here

I admit that this is not an effective method for this. but this is done with the standard gui provided by java

+2

, :

  • wkhtmltopdf HTML PDF
  • convert ( ImageMagick) PDF PNG ( )
+1

You can upload your HTML file to WebView and take a screenshot pragmatically and save this screenshot to a file. You can replace the URL in this snippet with local files.

 import java.io.FileOutputStream;
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Picture;
 import android.os.Bundle;
 import android.view.Menu;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

  public class MainActivity extends Activity {
     WebView w ;

     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        w = new WebView(this);
        w.setWebViewClient(new WebViewClient(){
           public void onPageFinished(WebView view, String url){
                Picture picture = view.capturePicture();
                Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
                picture.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(b);
                picture.draw(c);
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream("mnt/sdcard/yahoo.jpg");
                    if(fos != null){
                       b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                       fos.close();
                    }
                }catch(Exception e){}
            }
        });
        setContentView(w);
        w.loadUrl("http://search.yahoo.com/search?p=android");
    }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
+1
source

All Articles