通过蓝牙打印机Android打印PDF文件

在我的项目中需要打印PDF文件通过蓝牙打印机。 我写了一个代码来打印通过PDF其罚款的文本,

但是我想在蓝牙打印机上打印PDF文件。

我的Java代码来打印文本

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.listView1); // listdata(lv); try { // we are goin to have three buttons for specific functions Button openButton = (Button) findViewById(R.id.open); Button sendButton = (Button) findViewById(R.id.send); Button closeButton = (Button) findViewById(R.id.close); Button btnco= (Button) findViewById(R.id.btnconnect); btnco.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { findBT(); openBT(); } catch (Exception ex) { } } }); myLabel = (TextView) findViewById(R.id.label); myTextbox = (EditText) findViewById(R.id.entry); // open bluetooth connection openButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent(MainActivity.this,NewAct.class)); } }); // send data typed by the user to be printed sendButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { sendData(); } catch (IOException ex) { } } }); // close bluetooth connection closeButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { closeBT(); } catch (IOException ex) { } } }); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * This will find a bluetooth printer device */ void findBT() { try { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { myLabel.setText("No bluetooth adapter available"); Toast.makeText(MainActivity.this, "No bluetooth available", Toast.LENGTH_LONG).show(); // startActivity(new Intent(MainActivity.this,NewAct.class)); } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetooth = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBluetooth, 0); Toast.makeText(this, "OPen", Toast.LENGTH_LONG).show(); //startActivity(new Intent(MainActivity.this,NewAct.class)); } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { // MP300 is the name of the bluetooth printer device if (device.getName().equals(NewAct.printer)) { //openBT(); mmDevice = device; break; } else { } } } { myLabel.setText("Bluetooth Device Found"); try { // Standard SerialPortService ID UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); beginListenForData(); myLabel.setText("Bluetooth Opened"); } catch (NullPointerException e) { e.printStackTrace(); } } } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * Tries to open a connection to the bluetooth printer device */ void openBT() throws IOException { try { // Standard SerialPortService ID UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); beginListenForData(); myLabel.setText("Bluetooth Opened"); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * After opening a connection to bluetooth printer device, * we have to listen and check if a data were sent to be printed. */ void beginListenForData() { try { final Handler handler = new Handler(); // This is the ASCII code for a newline character final byte delimiter = 10; stopWorker = false; readBufferPosition = 0; readBuffer = new byte[1024]; workerThread = new Thread(new Runnable() { public void run() { while (!Thread.currentThread().isInterrupted() && !stopWorker) { try { int bytesAvailable = mmInputStream.available(); if (bytesAvailable > 0) { byte[] packetBytes = new byte[bytesAvailable]; mmInputStream.read(packetBytes); for (int i = 0; i < bytesAvailable; i++) { byte b = packetBytes[i]; if (b == delimiter) { byte[] encodedBytes = new byte[readBufferPosition]; System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); final String data = new String( encodedBytes, "US-ASCII"); readBufferPosition = 0; handler.post(new Runnable() { public void run() { myLabel.setText(data); } }); } else { readBuffer[readBufferPosition++] = b; } } } } catch (IOException ex) { stopWorker = true; } } } }); workerThread.start(); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * This will send data to be printed by the bluetooth printer */ void sendData() throws IOException { try { // the text typed by the user String msg = myTextbox.getText().toString(); msg += "\n"; mmOutputStream.write(msg.getBytes()); // tell the user data were sent myLabel.setText("Data Sent"); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * Close the connection to bluetooth printer. */ void closeBT() throws IOException { try { stopWorker = true; mmOutputStream.close(); mmInputStream.close(); mmSocket.close(); myLabel.setText("Bluetooth Closed"); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public void listdata(ListView lv){ try{ pairedDevices = mBluetoothAdapter.getBondedDevices(); ArrayList<String> list = new ArrayList<String>(); for(BluetoothDevice bt : pairedDevices) list.add(bt.getName()); Toast.makeText(getApplicationContext(),"Showing Paired Devices", Toast.LENGTH_SHORT).show(); @SuppressWarnings("unchecked") final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); }catch(Exception e) { Toast.makeText(this, "error"+e, Toast.LENGTH_LONG).show(); } } 

我没有想法如何通过PDF文件打印到蓝牙打印机

请帮助我如何做到这一点

提前致谢。

Solutions Collecting From Web of "通过蓝牙打印机Android打印PDF文件"

如果可以从另一个应用程序打印PDF(可能是制造商自己的?),可以尝试使用Android意图打开PDF。

 Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/pdf"); c.startActivity(intent); 

您可以尝试从PDF文件获取字节并将其发送到打印机,如下所示:

 /* * This will send data to be printed by the bluetooth printer */ void sendData() throws IOException { try { // the text typed by the user //String msg = myTextbox.getText().toString(); //msg += "\n"; InputStream is = this.openFileInput("filename.pdf"); // Where this is Activity ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; while ((int bytesRead = is.read(b)) != -1) { bos.write(b, 0, bytesRead); } byte[] bytes = bos.toByteArray(); byte[] printformat = { 27, 33, 0 }; //try adding this print format mmOutputStream.write(printformat); mmOutputStream.write(bytes); // tell the user data were sent myLabel.setText("Data Sent"); closeBT(); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } 

您需要在Android Manifest中添加以下权限:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

大多数蓝牙打印机不支持从盒子打印PDF。

你需要做的是使用Android打印机框架或任何其他类似PDFRenderer的库/类来将PDF页面转换为图像,并通过打印机可以理解的模拟(例如ZPL,CPCL,PCL-3等等)将这些图像发送到打印机。 )

任何应用程序打印PDF有一些库解码PDF。

当然,你可以只发送一个文本到你的打印机。

如果你想直接打印pdf,你需要自己的图书馆来做到这一点。

对于android我总是使用mupdf,非常powerfuf lib和开源。

看看stackoverflow如何安装一步一步在应用程序中集成MuPDF阅读器

在完成从https://github.com/derek-watson/mupdf运行完整的演示项目&#x540E;

目录列表(mupdf / platform / android /)

它会运行主要活动ChoosePDFActivity.java从你的设备拿起PDF后,它会显示,然后主顶栏有打印button(PrintDialogActivity.java打印在谷歌云打印)更改代码在这里发送stream到您的打印机

希望这个帮助!