附件有文件和SHX字体
font.zip (4.6 MB)
乱码文件.zip (52.5 KB)
public static void dwgToPdf (String uuid, String fileName, String fileExtension) throws IOException
{
// 根据uuid得到文件在服务器的存储路径
String zipFilePath = StringUtils.format(“{}/{}”, getUploadPath(), uuid);
List fontPaths = new ArrayList<>();
File shxFontPath = new File(RuoYiConfig.getShxFontsPath());
if(!shxFontPath.exists()){
shxFontPath.mkdirs();
}
File[] fonts = shxFontPath.listFiles();
if(!Objects.isNull(fonts) && fonts.length > 0){
fontPaths = Arrays.stream(fonts).map(File::getPath).collect(Collectors.toList());
}
// 如果文件不存在抛出异常
File file = new File(zipFilePath);
if (!file.exists())
{
throw new FileNotFoundException("文件在服务器上不存在!");
}
try(ZipFile zipFile = new ZipFile(zipFilePath))
{
List<FileHeader> headers = zipFile.getFileHeaders();
if(headers.stream().noneMatch(x -> x.getFileName().toLowerCase().endsWith(".dwg")))
{
throw new FileNotFoundException("文件异常!");
}
if(headers.stream().anyMatch(x -> x.getFileName().toLowerCase().endsWith(".pdf")))
{
zipFile.removeFile(StringUtils.format("{}{}",fileName,".pdf"));
}
try(
net.lingala.zip4j.io.inputstream.ZipInputStream zis = zipFile.getInputStream(zipFile.getFileHeader(StringUtils.format("{}{}",fileName,fileExtension)));
BufferedInputStream bis = new BufferedInputStream(zis);
ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
)
{
LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.Utf8);
Image image = Image.load(bis, opts);
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
cadRasterizationOptions.setScaleMethod(ScaleType.GrowToFit);
// cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
if(!fontPaths.isEmpty()){
cadRasterizationOptions.setShxFonts(fontPaths.toArray(new String[0]));
}
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
image.save(bos, pdfOptions);
bos.flush();
ZipParameters parameters = new ZipParameters();
parameters.setFileNameInZip(StringUtils.format("{}{}",fileName,".pdf"));
try(
InputStream is = new ByteArrayInputStream(os.toByteArray());
BufferedInputStream bis2 = new BufferedInputStream(is)
)
{
zipFile.addStream(bis2, parameters);
}
catch (Exception e)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
}