调用PDF的打印命令

调用PDF的打印命令,但是这种情况下是调用得服务器端的打印机环境,也就特定环境下能用。

FileOutputStream fosw = new FileOutputStream(new File("c:/a.pdf"));
		byteStream.writeTo(fosw);
		byteStream.close();
		fosw.close();
		
		try{
		String osName = System.getProperty("os.name" );
//		FOR WINDOWS 95 AND 98 USE COMMAND.COM
		if(osName.equals("Windows 95") || osName.equals("Windows 98")){
		Runtime.getRuntime().exec(
		"command.com /C start AcroRd32 /p /h \"C:/Documents and Settings/duanqingfeng/My Documents/cpcc_result_1.pdf\"");
		}
//		FOR WINDOWS NT/XP/2000 USE CMD.EXE
		else {
		Runtime.getRuntime().exec(
		"cmd.exe  /C  start acrord32  /p /h C:/a.pdf");
		
		}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

 

在Python中,我们可以使用`subprocess`模块来调用操作系统的默认打印打印本地PDF文件。这里有一个基本的例子,假设PDF文件名为`example.pdf`: ```python import os import subprocess def print_pdf(file_path): try: # 使用os.system()或subprocess.run()来调用系统命令 # 在Windows上通常使用"rundll32 printui.dll,PrintUIEntry /n /p:"加打印机名",例:" + file_path # 在Linux或macOS上可能使用lp或 cups 等命令,具体取决于你的系统设置 # 这里仅给出通用的Windows命令作为例子 command = f'rundll32 printui.dll,PrintUIEntry /n /p:"Your_Printer_Name" "{file_path}"' # 或者使用subprocess.run # subprocess.run(command, shell=True) # shell=True允许执行shell命令 # 调用操作系统命令,注意这里的shell=True表示该字符串被视为shell命令 # 如果不想使用shell,可以使用下面这种方式,但需要将命令拆分成list # cmd_list = ['rundll32', 'printui.dll', 'PrintUIEntry', '/n', '/p:', '"Your_Printer_Name"', file_path] # subprocess.run(cmd_list) # 执行命令,忽略输出(如果你想要输出,可以去掉"stdout=subprocess.DEVNULL") subprocess.call(command, shell=True, stdout=subprocess.DEVNULL) except Exception as e: print(f"Error occurred while printing PDF: {e}") # 调用函数,传入PDF文件路径 print_pdf("path_to_your_pdf/example.pdf") ``` 请注意替换`"Your_Printer_Name"`为你电脑上的实际打印机名称。这个代码片段在默认打印机上打开PDF并开始打印
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值