Sara Balloiuk
Sara Balloiuk

Reputation: 279

HTML file convert it to image file locally

I have a.html file which contains a table and I want to convert and save it locally as image.

Is it possible?

In order to save it as image I tried this:

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

a library to convert html to image but it is for online html page.

Upvotes: 4

Views: 2293

Answers (3)

Luboš Staráček
Luboš Staráček

Reputation: 907

You can load your HTML file into a WebView and do a screenshot pragrammatically and save that screenshot into a file. You can replace URL in that snippet to your 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;
   }

Upvotes: 1

afzalex
afzalex

Reputation: 8652

Here is a program using swings provided 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 (the undecorated 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 this is not an efficient method to do this. but it is done with standard gui provided by java

Upvotes: 2

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36987

You can do that by using a combination of command line programs:

  • wkhtmltopdf turns your HTML file into a PDF file
  • convert (from ImageMagick) turns PDF into PNG (or other image file formats)

Upvotes: 1

Related Questions