Convert PDF to PDF/A format Args: input_path: Path to source PDF file output_path: Path to save PDF/A file
(input_path, output_path)
| 251 | |
| 252 | |
| 253 | def convert_to_pdfa(input_path, output_path): |
| 254 | """ |
| 255 | Convert PDF to PDF/A format |
| 256 | |
| 257 | Args: |
| 258 | input_path: Path to source PDF file |
| 259 | output_path: Path to save PDF/A file |
| 260 | """ |
| 261 | from pikepdf import Dictionary, Name, Pdf |
| 262 | |
| 263 | # Open the PDF file |
| 264 | pdf = Pdf.open(input_path) |
| 265 | |
| 266 | # Add PDF/A conformance metadata |
| 267 | metadata = { |
| 268 | "pdfa_part": "2", |
| 269 | "pdfa_conformance": "B", |
| 270 | "title": pdf.docinfo.get("/Title", ""), |
| 271 | "author": pdf.docinfo.get("/Author", ""), |
| 272 | "creator": "PDF Math Translate", |
| 273 | } |
| 274 | |
| 275 | with pdf.open_metadata() as meta: |
| 276 | meta.load_from_docinfo(pdf.docinfo) |
| 277 | meta["pdfaid:part"] = metadata["pdfa_part"] |
| 278 | meta["pdfaid:conformance"] = metadata["pdfa_conformance"] |
| 279 | |
| 280 | # Create OutputIntent dictionary |
| 281 | output_intent = Dictionary( |
| 282 | { |
| 283 | "/Type": Name("/OutputIntent"), |
| 284 | "/S": Name("/GTS_PDFA1"), |
| 285 | "/OutputConditionIdentifier": "sRGB IEC61966-2.1", |
| 286 | "/RegistryName": "http://www.color.org", |
| 287 | "/Info": "sRGB IEC61966-2.1", |
| 288 | } |
| 289 | ) |
| 290 | |
| 291 | # Add output intent to PDF root |
| 292 | if "/OutputIntents" not in pdf.Root: |
| 293 | pdf.Root.OutputIntents = [output_intent] |
| 294 | else: |
| 295 | pdf.Root.OutputIntents.append(output_intent) |
| 296 | |
| 297 | # Save as PDF/A |
| 298 | pdf.save(output_path, linearize=True) |
| 299 | pdf.close() |
| 300 | |
| 301 | |
| 302 | def translate( |