Can't print anything using pdfbox
I am trying to use apache pdfbox to print a pdf file, so I created a controller responsible for printing any PDF file. As you can see from the code below, the controller only has one public method with a file path parameter. The controller works without exception, but prints nothing:
public class ControladorImpressao {
@Value("${nome.impressora}")
private String nomeImpressora;
private PDDocument arquivoPDF;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public boolean imprimir(String arquivo) {
try {
carregarArquivoPDF(arquivo);
iniciarImpressao(arquivo);
return true;
} catch (Exception e) {
logger.error("Erro ao tentar imprimir documento!",e);
}
return false;
}
private void carregarArquivoPDF(String arquivo) {
try {
arquivoPDF=new PDDocument();
arquivoPDF.load(arquivo);
}
catch (Exception e) {
logger.error("Erro ao abrir pdf!",e);
}
}
private void iniciarImpressao(String nomeArquivo) throws PrinterException {
PrintService impressora=recuperarImpressora();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(impressora);
job.setJobName(nomeArquivo);
job.setCopies(1);
arquivoPDF.silentPrint(job);
}
private PrintService recuperarImpressora() {
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (int count = 0; count < printServices.length; ++count) {
if (nomeImpressora.equalsIgnoreCase(printServices[count].getName())) {
return printServices[count];
}
}
return null;
}
}
I am using pdfbox version 1.7.0 with maven:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.7.0</version>
</dependency>
What am I doing wrong?
source to share
Solved the problem by changing the way I install the PDDocument (I'm using static load now) and changing how I use the PrinterJob:
public class ControladorImpressao {
@Value("${nome.impressora}")
private String nomeImpressora;
private PDDocument arquivoPDF;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public boolean imprimir(String arquivo) {
try {
arquivoPDF=PDDocument.load(new File(arquivo));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(recuperarImpressora());
job.setJobName(arquivo);
job.setPageable(arquivoPDF);
job.print();
return true;
} catch (Exception e) {
logger.error("Erro ao tentar imprimir documento!",e);
}
return false;
}
private PrintService recuperarImpressora() {
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (int count = 0; count < printServices.length; ++count) {
if (nomeImpressora.equalsIgnoreCase(printServices[count].getName())) {
return printServices[count];
}
}
return null;
}
}
An interesting thing I noticed is that if I use a static load method instead, I use:
arquivoPDF=new PDDocument(); arquivoPDF.load(arquivo);
I still can't print anything, maybe the problem is in the download method. Thanks to @yms, if he hadn't told me about this note in the PdfBox documentation, I might have gone a different route.
source to share