PDF文件包含一些法语重音字符,例如:e上面带有一些刻度标记,与u左右相同。
当我在打印前检查PDF文件时,我可以看到它们是完全正确的,我可以用PDF阅读它们,我可以放大并完全复制它们。
但Java接触它并打印它的那一刻。一切都没有按照预期的那样发挥作用。
我该如何解决?该PDF的云版本是完美的,但是一旦我的以下代码用于处理文件,那么所有这些字符在打印副本中都会被破坏。
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaTray;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
public class PrintA4 {
public static boolean saveFile(URL url, String file) throws IOException {
boolean download_status = false;
System.out.println("open");
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File(file));
System.out.println("reading file...");
int length = -1;
byte[] buffer = new byte[1024];
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
download_status = true;
System.out.println("downloaded");
return download_status;
}
public static void main(String[] args) throws IOException, PrinterException {
String downloaded_filename = "C:/pdf.pdf";
String download_pdf_from = "http://www.example.com/1.pdf" ;
String downloaded_filename_open_as_pdf = "C:\\pdf.pdf";
String printerNameDesired = "Brother HL-6180DW series";
PrintService[] services = PrinterJob.lookupPrintServices();
DocPrintJob docPrintJob = null;
for (int i = 0; i < services.length; i++) {
System.out.println(services[i]);
}
try{
URL url = new URL(download_pdf_from);
if(saveFile(url, downloaded_filename)) {
try {
PDDocument pdf = PDDocument.load(new File(downloaded_filename_open_as_pdf));
PrinterJob job = PrinterJob.getPrinterJob();
for (int i = 0; i < services.length; i++) {
if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
docPrintJob = services[i].createPrintJob();
}
}
job.setPrintService(docPrintJob.getPrintService());
job.setPageable(new PDFPageable(pdf));
PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet();
pset.add(MediaTray.BOTTOM);
job.print(pset);
} catch (Exception e) {
System.out.println("[FAIL]");
}
} else {
System.out.println("[FAIL]");
}
} catch (Exception ae) {
System.out.println("[FAIL]");
}
}
}