Skip to content
Demo示例FAQ

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

其他 PDF 标准转换 ​

除 PDF/A 外,ComPDF SDK 还支持将文档转换为 PDF/X、PDF/E、PDF/UA 和 PDF/VT 标准。转换方法与 PDF/A 相同,均使用 writeStandardPDFToFilePath:withType:iccFilePath:withUAConfig:isSaveFontSubset:error:,并通过 CPDFStandardMode 指定目标标准。

支持的标准

标准说明formattypenumpartType
PDF/X-1a印刷交换标准,仅允许 CMYK 和专色。CPDFStandardFormatPDFX1CPDFPartTypeNone
PDF/X-3印刷交换标准,允许使用 ICC 配置文件管理的 RGB 颜色。CPDFStandardFormatPDFX3CPDFPartTypeNone
PDF/X-4印刷交换标准,允许透明度和图层。CPDFStandardFormatPDFX4CPDFPartTypeNone
PDF/E-1工程文档标准。CPDFStandardFormatPDFE1CPDFPartTypeNone
PDF/UA-1无障碍访问标准,需要传入 CPDFUAConfig。CPDFStandardFormatPDFUA1CPDFPartTypeNone
PDF/VT-1可变数据与事务打印标准。CPDFStandardFormatPDFVT1CPDFPartTypeNone
PDF/VT-2可变数据与事务打印标准。CPDFStandardFormatPDFVT2CPDFPartTypeNone

ICC 配置文件

iccFilePath 为可选参数。传 nil 时,转换为 PDF/X 使用 SDK 内置的 CoatedFOGRA39 配置文件,转换为其他标准使用内置的 sRGB2014 配置文件;如需使用自定义配置文件(例如印刷厂提供的输出配置文件),传入 ICC 文件的路径即可。

转换为 PDF/X-4

以下是将文档转换为 PDF/X-4 的示例代码:

swift
// 目标标准:PDF/X-4(PDF/X 没有 partType,级别仅由 typenum 指定)
let standardMode = CPDFStandardMode()
standardMode.format = .PDFX
standardMode.typenum = 4
standardMode.partType = .none

// 自定义 ICC 配置文件路径;传 nil 时使用内置的 CoatedFOGRA39
let iccPath: String? = nil

do {
    try document.writeStandardPDF(toFilePath: savePath, withType: standardMode, iccFilePath: iccPath, with: nil, isSaveFontSubset: false, error: ())
} catch {
    print("Conversion failed: \((error as NSError).localizedFailureReason ?? "")")
}
objective-c
// 目标标准:PDF/X-4(PDF/X 没有 partType,级别仅由 typenum 指定)
CPDFStandardMode *standardMode = [[CPDFStandardMode alloc] init];
standardMode.format = CPDFStandardFormatPDFX;
standardMode.typenum = 4;
standardMode.partType = CPDFPartTypeNone;

// 自定义 ICC 配置文件路径;传 nil 时使用内置的 CoatedFOGRA39
NSString *iccPath = nil;

NSError *error = nil;
if (![document writeStandardPDFToFilePath:savePath
                                 withType:standardMode
                              iccFilePath:iccPath
                             withUAConfig:nil
                         isSaveFontSubset:NO
                                    error:&error]) {
    NSLog(@"Conversion failed: %@", error.localizedFailureReason);
}

转换为 PDF/UA-1

PDF/UA 要求文档具有标题和自然语言,因此转换时需要传入 CPDFUAConfig:

  • title:文档标题,不能为空,否则转换失败。
  • lauguage:文档语言,取值为 CPDFConvertLauguage(注意属性名和枚举名的拼写均为 Lauguage)。

以下是将文档转换为 PDF/UA-1 的示例代码:

swift
// 目标标准:PDF/UA-1
let standardMode = CPDFStandardMode()
standardMode.format = .PDFUA
standardMode.typenum = 1
standardMode.partType = .none

// PDF/UA 需要文档标题(不能为空)和语言
let uaConfig = CPDFUAConfig()
uaConfig.title = "ComPDF PDF/UA Sample"
uaConfig.lauguage = .EN_US

do {
    try document.writeStandardPDF(toFilePath: savePath, withType: standardMode, iccFilePath: nil, with: uaConfig, isSaveFontSubset: false, error: ())
} catch {
    print("Conversion failed: \((error as NSError).localizedFailureReason ?? "")")
}
objective-c
// 目标标准:PDF/UA-1
CPDFStandardMode *standardMode = [[CPDFStandardMode alloc] init];
standardMode.format = CPDFStandardFormatPDFUA;
standardMode.typenum = 1;
standardMode.partType = CPDFPartTypeNone;

// PDF/UA 需要文档标题(不能为空)和语言
CPDFUAConfig *uaConfig = [[CPDFUAConfig alloc] init];
uaConfig.title = @"ComPDF PDF/UA Sample";
uaConfig.lauguage = CPDFConvertLauguageEN_US;

NSError *error = nil;
if (![document writeStandardPDFToFilePath:savePath
                                 withType:standardMode
                              iccFilePath:nil
                             withUAConfig:uaConfig
                         isSaveFontSubset:NO
                                    error:&error]) {
    NSLog(@"Conversion failed: %@", error.localizedFailureReason);
}

注: PDF/E 仅支持转换为 PDF/E-1;PDF/E-2 目前只支持验证,不支持转换。