title: How to Print a Web Page from Webview/URL /PDF Using Thermal Printer(EPSON)?
body: <p>I followed <a href="https://download.epson-biz.com/modules/pos/index.php?page=soft&amp;scat=61" rel="nofollow">this</a>/<a href="https://download.epson-biz.com/modules/pos/index.php?page=single_soft&amp;cid=4747&amp;scat=61&amp;pcat=52" rel="nofollow">this</a> to Print Receipts in part of POS(Point of Sale) from EPSON Printer</p>
  
  <p>I used Epson TM T20 Printer... So here I can print from Network and USB..
  And Its working Fine...</p>
  
  <p>This is my code :</p>
  
  <pre><code>public class MainActivity extends Activity implements View.OnClickListener, ReceiveListener {
  
  private Context mContext = null;
  private EditText mEditTarget = null;
  private Spinner mSpnSeries = null;
  private Spinner mSpnLang = null;
  private Printer  mPrinter = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      mContext = this;
  
      int[] target = {
          R.id.btnDiscovery,
          R.id.btnSampleReceipt,
     };
  
      for (int i = 0; i &lt; target.length; i++) {
          Button button = (Button)findViewById(target[i]);
          button.setOnClickListener(this);
      }
  
      mSpnSeries = (Spinner)findViewById(R.id.spnModel);
      ArrayAdapter&lt;SpnModelsItem&gt; seriesAdapter = new ArrayAdapter&lt;SpnModelsItem&gt;(this, android.R.layout.simple_spinner_item);
      seriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      seriesAdapter.add(new SpnModelsItem(getString(R.string.printerseries_t20), Printer.TM_T20));
      mSpnSeries.setAdapter(seriesAdapter);
      mSpnSeries.setSelection(0);
  
      try {
          Log.setLogSettings(mContext, Log.PERIOD_TEMPORARY, Log.OUTPUT_STORAGE, null, 0, 1, Log.LOGLEVEL_LOW);
      }
      catch (Exception e) {
          ShowMsg.showException(e, "setLogSettings", mContext);
      }
      mEditTarget = (EditText)findViewById(R.id.edtTarget);
  }
  
  @Override
  protected void onActivityResult(int requestCode, final int resultCode, final Intent data) {
      if (data != null &amp;&amp; resultCode == RESULT_OK) {
          String target = data.getStringExtra(getString(R.string.title_target));
          if (target != null) {
              EditText mEdtTarget = (EditText)findViewById(R.id.edtTarget);
              mEdtTarget.setText(target);
          }
      }
  }
  
  @Override
  public void onClick(View v) {
      Intent intent = null;
  
      switch (v.getId()) {
          case R.id.btnDiscovery:
              intent = new Intent(this, DiscoveryActivity.class);
              startActivityForResult(intent, 0);
              break;
  
          case R.id.btnSampleReceipt:
              updateButtonState(false);
              if (!runPrintReceiptSequence()) {
                  updateButtonState(true);
              }
              break;
  
          case R.id.btnSampleCoupon:
              updateButtonState(false);
              if (!runPrintCouponSequence()) {
                  updateButtonState(true);
              }
              break;
  
          default:
              // Do nothing
              break;
      }
  }
  
  private boolean runPrintReceiptSequence() {
      if (!initializeObject()) {
          return false;
      }
  
      if (!createReceiptData()) {
          finalizeObject();
          return false;
      }
  
      if (!printData()) {
          finalizeObject();
          return false;
      }
  
      return true;
  }
  
  private boolean createReceiptData() {
      String method = "";
      Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.store);
      StringBuilder textData = new StringBuilder();
      final int barcodeWidth = 2;
      final int barcodeHeight = 100;
  
      if (mPrinter == null) {
          return false;
      }
  
      try {
          method = "addTextAlign";
          mPrinter.addTextAlign(Printer.ALIGN_CENTER);
  
          method = "addImage";
          mPrinter.addImage(logoData, 0, 0,
                            logoData.getWidth(),
                            logoData.getHeight(),
                            Printer.COLOR_1,
                            Printer.MODE_MONO,
                            Printer.HALFTONE_DITHER,
                            Printer.PARAM_DEFAULT,
                            Printer.COMPRESS_AUTO);
  
          method = "addFeedLine";
          mPrinter.addFeedLine(1);
          textData.append("EPSON PRINT DEMO TEST - (HYD)\n");
          textData.append("STORE DIRECTOR – MLN\n");
          textData.append("\n");
          textData.append("07/06/12 09:15 012 0191 134\n");
          textData.append("ST# 21 OP# 001 TE# 01 TR# 747\n");
          textData.append("------------------------------\n");
          method = "addText";
          mPrinter.addText(textData.toString());
          textData.delete(0, textData.length());
  
          textData.append("524 3 CUP BLK TEAPOT    9.99 R\n");
          textData.append("003 WESTGATE BLACK 25  59.99 R\n");
          textData.append("------------------------------\n");
          method = "addText";
          mPrinter.addText(textData.toString());
          textData.delete(0, textData.length());
  
          textData.append("SUBTOTAL                69.98\n");
          textData.append("TAX                      14.43\n");
          method = "addText";
          mPrinter.addText(textData.toString());
          textData.delete(0, textData.length());
  
          method = "addTextSize";
          mPrinter.addTextSize(2, 2);
          method = "addText";
          mPrinter.addText("TOTAL    84.41\n");
          method = "addTextSize";
          mPrinter.addTextSize(1, 1);
          method = "addFeedLine";
          mPrinter.addFeedLine(1);
  
          textData.append("CASH                    200.00\n");
          textData.append("CHANGE                   78.14\n");
          textData.append("------------------------------\n");
          method = "addText";
          mPrinter.addText(textData.toString());
          textData.delete(0, textData.length());
  
          textData.append("Purchased item total number\n");
          textData.append("Sign Up and Save !\n");
          textData.append("With Preferred Saving Card\n");
  
          method = "addText";
          mPrinter.addText(textData.toString());
          textData.delete(0, textData.length());
          method = "addFeedLine";
          mPrinter.addFeedLine(2);
  
          method = "addCut";
          mPrinter.addCut(Printer.CUT_FEED);
  
  
      }
      catch (Exception e) {
          ShowMsg.showException(e, method, mContext);
          return false;
      }
  
      textData = null;
  
      return true;
  }
  
  private boolean runPrintCouponSequence() {
      if (!initializeObject()) {
          return false;
      }
  
      if (!createCouponData()) {
          finalizeObject();
          return false;
      }
  
      if (!printData()) {
          finalizeObject();
          return false;
      }
  
      return true;
  }
  
  
  private boolean printData() {
      if (mPrinter == null) {
          return false;
      }
  
      if (!connectPrinter()) {
          return false;
      }
  
      PrinterStatusInfo status = mPrinter.getStatus();
  
      dispPrinterWarnings(status);
  
      if (!isPrintable(status)) {
          ShowMsg.showMsg(makeErrorMessage(status), mContext);
          try {
              mPrinter.disconnect();
          }
          catch (Exception ex) {
              // Do nothing
          }
          return false;
      }
  
      try {
          mPrinter.sendData(Printer.PARAM_DEFAULT);
      }
      catch (Exception e) {
          ShowMsg.showException(e, "sendData", mContext);
          try {
              mPrinter.disconnect();
          }
          catch (Exception ex) {
              // Do nothing
          }
          return false;
      }
  
      return true;
  }
  
  private boolean initializeObject() {
      try {
          mPrinter = new Printer(((SpnModelsItem) mSpnSeries.getSelectedItem()).getModelConstant(),
                                 ((SpnModelsItem) mSpnLang.getSelectedItem()).getModelConstant(),
                                 mContext);
      }
      catch (Exception e) {
          ShowMsg.showException(e, "Printer", mContext);
          return false;
      }
  
      mPrinter.setReceiveEventListener(this);
  
      return true;
  }
  
  private void finalizeObject() {
      if (mPrinter == null) {
          return;
      }
  
      mPrinter.clearCommandBuffer();
  
      mPrinter.setReceiveEventListener(null);
  
      mPrinter = null;
  }
  
  private boolean connectPrinter() {
      boolean isBeginTransaction = false;
  
      if (mPrinter == null) {
          return false;
      }
  
      try {
          mPrinter.connect(mEditTarget.getText().toString(), Printer.PARAM_DEFAULT);
      }
      catch (Exception e) {
          ShowMsg.showException(e, "connect", mContext);
          return false;
      }
  
      try {
          mPrinter.beginTransaction();
          isBeginTransaction = true;
      }
      catch (Exception e) {
          ShowMsg.showException(e, "beginTransaction", mContext);
      }
  
      if (isBeginTransaction == false) {
          try {
              mPrinter.disconnect();
          }
          catch (Epos2Exception e) {
              // Do nothing
              return false;
          }
      }
  
      return true;
  }
  
  private void disconnectPrinter() {
      if (mPrinter == null) {
          return;
      }
  
      try {
          mPrinter.endTransaction();
      }
      catch (final Exception e) {
          runOnUiThread(new Runnable() {
              @Override
              public synchronized void run() {
                  ShowMsg.showException(e, "endTransaction", mContext);
              }
          });
      }
  
      try {
          mPrinter.disconnect();
      }
      catch (final Exception e) {
          runOnUiThread(new Runnable() {
              @Override
              public synchronized void run() {
                  ShowMsg.showException(e, "disconnect", mContext);
              }
          });
      }
  
      finalizeObject();
  }
  
  private boolean isPrintable(PrinterStatusInfo status) {
      if (status == null) {
          return false;
      }
  
      if (status.getConnection() == Printer.FALSE) {
          return false;
      }
      else if (status.getOnline() == Printer.FALSE) {
          return false;
      }
      else {
          ;//print available
      }
  
      return true;
  }
  
  private String makeErrorMessage(PrinterStatusInfo status) {
      String msg = "";
  
      if (status.getBatteryLevel() == Printer.BATTERY_LEVEL_0) {
          msg += getString(R.string.handlingmsg_err_battery_real_end);
      }
  
      return msg;
  }
  
  private void dispPrinterWarnings(PrinterStatusInfo status) {
      EditText edtWarnings = (EditText)findViewById(R.id.edtWarnings);
      String warningsMsg = "";
  
      if (status == null) {
          return;
      }
  
      if (status.getPaper() == Printer.PAPER_NEAR_END) {
          warningsMsg += getString(R.string.handlingmsg_warn_receipt_near_end);
      }
  
      if (status.getBatteryLevel() == Printer.BATTERY_LEVEL_1) {
          warningsMsg += getString(R.string.handlingmsg_warn_battery_near_end);
      }
  
      edtWarnings.setText(warningsMsg);
  }
  
  private void updateButtonState(boolean state) {
      Button btnReceipt = (Button)findViewById(R.id.btnSampleReceipt);
      Button btnCoupon = (Button)findViewById(R.id.btnSampleCoupon);
      btnReceipt.setEnabled(state);
      btnCoupon.setEnabled(state);
  }
  
  @Override
  public void onPtrReceive(final Printer printerObj, final int code, final PrinterStatusInfo status, final String printJobId) {
      runOnUiThread(new Runnable() {
          @Override
          public synchronized void run() {
              ShowMsg.showResult(code, makeErrorMessage(status), mContext);
  
              dispPrinterWarnings(status);
  
              updateButtonState(true);
  
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      disconnectPrinter();
                  }
              }).start();
          }
      });
  }
  }
  </code></pre>
  
  <p>So Here I want to print A Web page.. For that I have added a Webview... when I click on web view It need to Print Entire Web Page...</p>
  
  <p>I else When I enter URL It need to Print entire web page....</p>
  
  <p>I followed <a href="https://play.google.com/store/apps/details?id=com.custom.customprint&amp;hl=en" rel="nofollow">this</a> Application... Actually In this we can print a Web page... weather it may be image or text or any thing ... we can print complete web page,,,,</p>
  
  <p>I have seen source code of the above application but I am unable to use it...</p>
  
  <p>What ever I have understand that Its converting webpage as image and saving in temp location and printing...</p>
  
  <p>But I am Unable to find this kind in EPSON or any other thermal printer can any one suggest me... How to Print Complete Web page...</p>
  
  <p>I think that app is making a image of webpage and printing ,,, can any one suggest me how to use this in Epson...</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Here I am getting Data exact Printed format of epson from url... I want to Print it... with my app.. can any one suggest.. How to print form URL or Webview...</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>Here I am getting data Json from URL..Inside the Json Object I am getting Html print templete... like this..</p>
  
  <pre><code>{
      "response": {
          "status": "&lt;table&gt;.... &lt;/table&gt;"
      }
  }
  </code></pre>
  
  <p>so with intent I used the Above json response to a string and converted it to html...</p>
  
  <pre><code>        method = "addFeedLine";
          mPrinter.addFeedLine(1);
  
          textData.append("Test print Sample stirng\n");**//this is sample text**
  
          textData.append(Html.fromHtml(status + "\n"));**//this is JSON response which is northing but HTML code, SO I am converting it to stirng**
  </code></pre>
  
  <p>So my problem is I getting plain text.. there is no format which is equal to printer text...
  before that I got complete html code so I used... above line to convert html to sting...</p>
  
  <p><strong>Is there any way to Print from PDF or Image... from URL</strong>
  I can convert that receipt to image or pdf online..
  But I want to print it in thermal printer...</p>
  
  <p>Please suggest me regarding... this.... there is not article related this... please help me...</p>