草庐IT

关于java:PDF/A代字体编码的问题

codeneng 2023-03-28 原文

Problem about font encoding in PDF/A generation

所以这是我的问题:
我目前正在开发一个将文档归档为 PDF/A-1 的 java 应用程序。我正在使用 PdfBox 生成 pdf,但由于字体的原因,我无法生成有效的 PDF/A-1 pdf。字体嵌入在 pdf 文件中,但该网站:https://www.pdf-online.com/osa/validate.aspx 告诉我这不是有效的 PDF/A,因为:

The key Encoding has a value Identity-H which is prohibited.

我在互联网上查看了这个 Identity-H 编码是什么,它似乎是字体的编码方式,就像 ansi 编码一样。

我已经尝试过使用不同的字体,如 Helvetica 或 arial unicode Ms 但没有任何效果,总是有这种 Identity-H 编码。我对编码中的所有这些混乱感到有点迷茫,所以如果有人可以解释一下会很棒的。这也是我编写的在 pdf 中嵌入字体的代码:

1
2
3
4
5
6
7
8
 // load the font as this needs to be embedded
PDFont font = PDType0Font.load(doc, getClass().getClassLoader().getResourceAsStream(fontfile), true);

if (!font.isEmbedded())
        {
            throw new IllegalStateException("PDF/A compliance requires that all fonts used for"
                    +" text rendering in rendering modes other than rendering mode 3 are embedded.");
        }

感谢您的帮助:)

  • 这很奇怪 - 使用 CreatePDFA 示例生成的 PDF 在那里验证。
  • 与问题无关:不要使用 "getClassLoader()",这会给某些 java 版本带来问题。问题.apache.org/jira/browse/PDFBOX-4428
  • 您能否 1) 自己尝试 CreatePDFA 示例,2) 共享未验证的文件?
  • @TilmanHausherr 我使用了 Apache 的示例,它可以工作!我不知道为什么,因为文件和代码是但是,如果它工作。我关于 PDF/A 生成的最后一个问题是处理嵌入式文件。我尝试制作 PDF/A3-a,但在验证它时出现了最后一个错误:"需要密钥 AFRelationship,但缺少"。我在网上查了一下,但没有人说如何在 pdfbox 中设置它。
  • 使用 dictionary.setItem(COSName.getPDFName("AFRelationship"), COSName.getPDFName("Supplement")) 或任何正确的值。 (可以是 Source、Data、Alternative、Supplement、EncryptedPayload、FormData、Schema 或 Unspecified)
  • @TilmanHausherr 字典的对象类型是什么?
  • 一个COSDictionary。您通常通过在元素上调用 getCOSObject() 来获得它。 (该文件附件结构中的一些元素。最好是使用 PDFDebugger 打开现有的 PDF/A-3 文件以查看它是哪一个)
  • @TilmanHausherr 非常感谢它的工作!我有最后一个问题。我没有看到任何人在谈论它,所以如果你能帮助我,那就太好了!尝试验证 PDF/A 时出现此错误:"文件规范 \\'Test.xlsx\\' 未与对象关联。"。我使用 PDF/A 的 3-a 版本来允许嵌入文件我没有看到任何解决方案来修复它。谢谢您帮忙 :)
  • 我没有直接的答案,但请参见此处:mail-archives.apache.org/mod_mbox/pdfbox-users/201312.mbox/... 和 github.com/veraPDF/veraPDF-validation-profiles/wiki/... " PDF/A-3 文档中的每个文件附件都必须从 PDF 文档中的以下对象之一引用"如果这没有帮助我建议您创建一个新问题。
  • 回复原来的问题,我建议你自己回答。我仍然想知道您的 PDF 和示例 PDF 之间的区别是什么。


问题已解决:

我使用了 apache 的示例:CreatePDFA(我不知道为什么它会起作用,而不是我的代码):examples/src/main/java/org/apache/pdfbox/examples 中的示例

我添加以符合 PDF/A-3 要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
doc.getDocumentCatalog().setLanguage("en-US");

PDMarkInfo mark = new PDMarkInfo(); // new PDMarkInfo(page.getCOSObject());
PDStructureTreeRoot treeRoot = new PDStructureTreeRoot();
doc.getDocumentCatalog().setMarkInfo(mark);
doc.getDocumentCatalog().setStructureTreeRoot(treeRoot);
doc.getDocumentCatalog().getMarkInfo().setMarked(true);

PDDocumentInformation info = doc.getDocumentInformation();
info.setCreationDate(date);
info.setModificationDate(date);
info.setAuthor("KairosPDF");
info.setProducer("KairosPDF");
info.setCreator("KairosPDF");
info.setTitle("Generated PDf");
info.setSubject("PDF/A3-A");

这是我将文件嵌入到 pdf 的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private final PDDocument doc = new PDDocument();
private final PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
private final PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
private final Map<String, PDComplexFileSpecification> efMap = new HashMap<>();

public void addFile(PDDocument doc, File child) throws IOException {
    File file = new File(child.getPath());

    Calendar date = Calendar.getInstance();

    //first create the file specification, which holds the embedded file
    PDComplexFileSpecification fs = new PDComplexFileSpecification();
    fs.setFileUnicode(child.getName());
    fs.setFile(child.getName());
    InputStream is = new FileInputStream(file);
    PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);

    //Setting
    ef.setSubtype("application/octet-stream");
    ef.setSize((int) file.length() + 1);
    ef.setCreationDate(date);
    ef.setModDate(date);
    COSDictionary dictionary = fs.getCOSObject();
    dictionary.setItem(COSName.getPDFName("AFRelationship"), COSName.getPDFName("Data"));

    fs.setEmbeddedFile(ef);

    efMap.put(child.getName(), fs);
    efTree.setNames(efMap);

    names.setEmbeddedFiles(efTree);
    doc.getDocumentCatalog().setNames(names);
    is.close();
}

剩下的唯一问题是验证中的这个错误:

File specification 'Test.txt' not associated with an object.

希望对大家有所帮助。

有关关于java:PDF/A代字体编码的问题的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  5. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  6. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  7. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. ruby-on-rails - Prawn PDF : I need to generate nested tables - 2

    我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested

  10. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

随机推荐