如何在热敏打印机中打印PDF?从URL /本地到打印机的PDF

时间:2016-06-28 06:31:43

标签: android pdf printing thermal-printer

我关注了Epson SDK并使用了示例代码...打印收据

所以在那里,我得到的样本我可以打印文本,图像和两种方式...... 通过使用此...来打印文本和图像..

    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 < target.length; i++) {
        Button button = (Button)findViewById(target[i]);
        button.setOnClickListener(this);
    }

    mSpnSeries = (Spinner)findViewById(R.id.spnModel);
    ArrayAdapter<SpnModelsItem> seriesAdapter = new ArrayAdapter<SpnModelsItem>(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 && 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 - \n");
        textData.append("STORE DIRECTOR – XYZ\n");
        textData.append("\n");
        textData.append("28/06/16 05:15 012 0154  0225\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 5 GREEN TEA        19.99 R\n");
        textData.append("003 2 LEMON TEA        59.99 R\n");
        textData.append("------------------------------\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        textData.append("SUBTOTAL                 79.98\n");
        textData.append("TAX                      15.00\n");
        method = "addText";
        mPrinter.addText(textData.toString());
        textData.delete(0, textData.length());

        method = "addTextSize";
        mPrinter.addTextSize(2, 2);
        method = "addText";
        mPrinter.addText("TOTAL    94.98.41\n");
        method = "addTextSize";
        mPrinter.addTextSize(1, 1);
        method = "addFeedLine";
        mPrinter.addFeedLine(1);

        textData.append("CASH                    100.00\n");
        textData.append("CHANGE                    5.02\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();
        }
     });
    }
    }

我想以同样的方式打印PDF。这可能没有任何库吗?

否则是否可以在热敏打印机中打印PDF?

有人可以建议我如何在热敏打印机中打印PDF吗?

我在这里打印图像/位图

Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.store);
StringBuilder textData = new StringBuilder();
final int barcodeWidth = 2;
final int barcodeHeight = 100;

使用addimage ..和文本我给的是纯文本...使用addtext ....

任何人都可以建议我如何为此添加PDF ...

我关注了5月的教程,...但是在热敏打印机(EPSON)打印机中找不到与PDF相关的内容。

0 个答案:

没有答案