Skip to content

Elixir API 参考

PDF Oxide 提供地道的 Elixir 绑定,底层通过 NIF 调用 pdf_oxide 的 C ABI。CPU 密集型提取运行在脏 CPU 调度器上,因此永远不会阻塞 BEAM;原生句柄由垃圾回收器自动释放(也可以通过对应的 *_close/1 函数手动立即释放)。

# mix.exs
def deps do
  [{:pdf_oxide, "~> 0.3.69"}]
end

Rust API 请参阅 Rust API 参考;Python 请参阅 Python API 参考;类型细节请参阅 类型与枚举

约定

  • 所有公开函数都位于单一的 PdfOxide 模块中。
  • 函数返回 {:ok, value}{:error, code}(一个整数错误码);Error 异常携带 codeop
  • 页索引从 0 开始
  • 坐标和矩形使用 PDF 用户空间点(原点位于左下角),除非另有说明。
  • 颜色通道为 0.01.0 范围内的浮点数(DeviceRGB)。
  • 图像/渲染 format 代码:0 = PNG,1 = JPEG。

结构体

以下结构体封装了原生资源句柄,或下方函数返回的解码后数据。

结构体 用途
PdfOxide.Document 已打开的可读取/可提取 PDF 句柄
PdfOxide.Pdf 已构建的 PDF 句柄(来自创建/转换)
PdfOxide.DocumentEditor 可变更的 PDF 编辑句柄
PdfOxide.Page 单个 0-based 页面的轻量视图
PdfOxide.DocumentBuilder PDF 创建构建器
PdfOxide.PageBuilder DocumentBuilder 启动的页面创建构建器
PdfOxide.EmbeddedFont 用于嵌入的已加载 TTF/OTF 字体
PdfOxide.Certificate 用于签名的 X.509 证书 + 私钥
PdfOxide.SignatureInfo 已解析的 PDF 签名
PdfOxide.Timestamp 已解析的 RFC 3161 时间戳令牌
PdfOxide.TsaClient RFC 3161 时间戳颁发机构客户端
PdfOxide.Dss 文档安全存储(Document Security Store)句柄
PdfOxide.PdfAResult / PdfUaResult / PdfXResult 校验结果
PdfOxide.UaStats 无障碍元素计数
PdfOxide.Barcode 生成/解码得到的条形码或二维码
PdfOxide.OcrEngine OCR 引擎句柄
PdfOxide.Renderer 可复用的页面渲染器
PdfOxide.ElementList 不透明的页面元素列表
PdfOxide.Error 携带 codeop 的异常

数据结构体

结构体 字段
PdfOxide.Bbox xywidthheight
PdfOxide.Char character(码位)、bboxfont_namefont_size
PdfOxide.Word textbboxfont_namefont_sizebold
PdfOxide.TextLine textbboxword_count
PdfOxide.Table row_countcol_counthas_headercells
PdfOxide.Font nametypeencodingembeddedsubset
PdfOxide.Image widthheightbits_per_componentformatcolorspacedata
PdfOxide.Annotation typesubtypecontentauthorrectborder_width
PdfOxide.Path bboxstroke_widthhas_strokehas_filloperation_count
PdfOxide.FormField namevaluetyperead_onlyrequired
PdfOxide.SearchResult textpagebbox
PdfOxide.RenderedImage refwidthheightdata

Document — 打开文档

PdfOxide.open(path)                          # {:ok, %Document{}}
PdfOxide.open_with_password(path, password)  # open an encrypted PDF
PdfOxide.open_from_bytes(bytes)              # open from an in-memory binary
PdfOxide.open_from_docx_bytes(bytes)         # open a DOCX as a Document
PdfOxide.open_from_pptx_bytes(bytes)         # open a PPTX as a Document
PdfOxide.open_from_xlsx_bytes(bytes)         # open an XLSX as a Document
PdfOxide.close(handle)                       # free a Document/Pdf/DocumentEditor handle

Document — 文档信息

PdfOxide.page_count(doc)            # number of pages
PdfOxide.version(doc)              # %{major: _, minor: _}
PdfOxide.encrypted?(doc)          # whether the document is encrypted
PdfOxide.structure_tree?(doc)     # whether it has a logical structure tree
PdfOxide.authenticate(doc, password)  # authenticate an encrypted document
PdfOxide.has_xfa?(doc)            # whether the document carries XFA forms
PdfOxide.source_bytes(doc)        # the original source PDF bytes
PdfOxide.page_width(doc, page)    # page width in points
PdfOxide.page_height(doc, page)   # page height in points
PdfOxide.page_rotation(doc, page) # page rotation in degrees

Document — 文本提取

PdfOxide.extract_text(doc, page)        # reading-order text for a page
PdfOxide.to_plain_text(doc, page)       # plain text for a page
PdfOxide.to_markdown(doc, page)         # Markdown for a page
PdfOxide.to_html(doc, page)             # HTML for a page
PdfOxide.to_plain_text_all(doc)         # plain text for the whole document
PdfOxide.to_markdown_all(doc)           # Markdown for the whole document
PdfOxide.to_html_all(doc)               # HTML for the whole document
PdfOxide.extract_structured_json(doc, page)  # structured page content as JSON
PdfOxide.extract_chars(doc, page)       # [%Char{}] with positioning + font metadata
PdfOxide.extract_words(doc, page)       # [%Word{}] with layout/style
PdfOxide.extract_text_lines(doc, page)  # [%TextLine{}]
PdfOxide.extract_tables(doc, page)      # [%Table{}]
PdfOxide.cell(table, row, col)          # 0-based cell text from a %Table{}
PdfOxide.extract_text_auto(doc, page)   # auto-pick the best extraction path for a page
PdfOxide.extract_all_text(doc)          # auto-extract text across all pages
PdfOxide.extract_page_auto(doc, page, options_json \\ "")  # auto-extract one page (JSON options)

Document — 区域提取

PdfOxide.extract_text_in_rect(doc, page, x, y, w, h)    # text inside a rectangle
PdfOxide.extract_words_in_rect(doc, page, x, y, w, h)   # [%Word{}] inside a rectangle
PdfOxide.extract_lines_in_rect(doc, page, x, y, w, h)   # [%TextLine{}] inside a rectangle
PdfOxide.extract_tables_in_rect(doc, page, x, y, w, h)  # [%Table{}] inside a rectangle
PdfOxide.extract_images_in_rect(doc, page, x, y, w, h)  # [%Image{}] inside a rectangle

Document — 字体、图像、注释、路径

PdfOxide.embedded_fonts(doc, page)    # [%Font{}] used on a page
PdfOxide.embedded_images(doc, page)   # [%Image{}] with raw bytes
PdfOxide.page_annotations(doc, page)  # [%Annotation{}]
PdfOxide.extract_paths(doc, page)     # [%Path{}] vector paths

Document — 页面分类与清理

PdfOxide.classify_page(doc, page)              # classify a single page
PdfOxide.classify_document(doc)                # classify the whole document
PdfOxide.erase_header(doc, page)               # erase the detected header on a page
PdfOxide.erase_footer(doc, page)               # erase the detected footer on a page
PdfOxide.erase_artifacts(doc, page)            # erase detected page artifacts
PdfOxide.remove_headers(doc, threshold \\ 0.5) # remove repeating headers document-wide
PdfOxide.remove_footers(doc, threshold \\ 0.5) # remove repeating footers document-wide
PdfOxide.remove_artifacts(doc, threshold \\ 0.5) # remove repeating artifacts document-wide

Document — 搜索

PdfOxide.search(doc, page, term, case_sensitive)      # [%SearchResult{}] on one page
PdfOxide.search_all(doc, term, case_sensitive)        # [%SearchResult{}] across all pages
PdfOxide.search_results_to_json(doc, term, case_sensitive \\ false)  # results as JSON

Document — 页面视图辅助函数

PdfOxide.page(doc, index)   # a lightweight %Page{} view of a 0-based page
PdfOxide.text(page)         # extracted text for a %Page{}
PdfOxide.markdown(page)     # Markdown for a %Page{}
PdfOxide.html(page)         # HTML for a %Page{}
PdfOxide.plain_text(page)   # plain text for a %Page{}

Document — 结构与元数据

PdfOxide.outline(doc)        # the document outline / bookmarks
PdfOxide.page_labels(doc)    # page label ranges
PdfOxide.xmp_metadata(doc)   # XMP metadata
PdfOxide.plan_split_by_bookmarks(doc, options_json \\ "")  # plan a split by bookmarks

Document — Office 输出

PdfOxide.to_docx(doc)   # convert the document to DOCX bytes
PdfOxide.to_pptx(doc)   # convert the document to PPTX bytes
PdfOxide.to_xlsx(doc)   # convert the document to XLSX bytes

Document — 表单

PdfOxide.form_fields(doc)                              # [%FormField{}]
PdfOxide.export_form_data_to_bytes(doc, format_type \\ 0)  # export form data (FDF/XFDF) to bytes
PdfOxide.import_form_data(doc, data_path)             # import form data from a file path
PdfOxide.form_import_from_file(doc, filename)         # import form data from a file

Document — 注释访问器

PdfOxide.annotation_color(doc, page, index)              # annotation RGB colour
PdfOxide.annotation_creation_date(doc, page, index)      # creation date
PdfOxide.annotation_modification_date(doc, page, index)  # modification date
PdfOxide.annotation_hidden?(doc, page, index)            # hidden flag
PdfOxide.annotation_marked_deleted?(doc, page, index)    # marked-deleted flag
PdfOxide.annotation_printable?(doc, page, index)         # printable flag
PdfOxide.annotation_read_only?(doc, page, index)         # read-only flag
PdfOxide.link_annotation_uri(doc, page, index)           # URI of a Link annotation
PdfOxide.text_annotation_icon_name(doc, page, index)     # icon name of a Text annotation
PdfOxide.highlight_quad_points_count(doc, page, index)   # quad-point count of a Highlight
PdfOxide.highlight_quad_point(doc, page, index, quad_index)  # one quad point of a Highlight
PdfOxide.annotations_to_json(doc, page)                  # all annotations as JSON

Document — 元素列表

PdfOxide.page_elements(doc, page)             # {:ok, %ElementList{}} of page elements
PdfOxide.element_count(list)                  # number of elements
PdfOxide.element_type(list, index)            # element type code
PdfOxide.element_text(list, index)            # element text
PdfOxide.element_rect(list, index)            # element rectangle
PdfOxide.elements_to_json(list)               # the element list as JSON
PdfOxide.element_list_close(list)             # free the element list handle
PdfOxide.font_size(doc, page, index)          # font size of a page font entry
PdfOxide.fonts_to_json(doc, page)             # page fonts as JSON

Document — 渲染

PdfOxide.render_page(doc, page_index, format \\ 0)              # render a page to image bytes
PdfOxide.render_page_zoom(doc, page_index, zoom, format \\ 0)   # render at a zoom factor
PdfOxide.render_page_thumbnail(doc, page_index, size, format \\ 0)  # render a square thumbnail
PdfOxide.render_page_with_options(doc, page, opts \\ [])        # render with dpi/background/jpeg_quality opts
PdfOxide.render_page_with_options_ex(doc, page, excluded_layers, opts \\ [])  # render, suppressing OCG layers
PdfOxide.render_page_region(doc, page, crop_x, crop_y, crop_width, crop_height, format \\ 0)  # render a region
PdfOxide.render_page_fit(doc, page, w, h, format \\ 0)          # render to fit w×h pixels
PdfOxide.render_page_raw(doc, page, dpi \\ 150)                 # render to a raw RGBA8888 buffer
PdfOxide.renderer(dpi \\ 150, format \\ 0, quality \\ 85, anti_alias \\ true)  # reusable %Renderer{}
PdfOxide.renderer_close(renderer)                              # free a renderer handle
PdfOxide.estimate_render_time(doc, page)                       # estimated render time (ms)

render_page_with_options/3 的关键字选项::dpi:format:background{r, g, b, a})、:transparent_background:render_annotations:jpeg_quality

Document — OCR

PdfOxide.ocr_engine(det_model_path, rec_model_path, dict_path)  # build an %OcrEngine{}
PdfOxide.ocr_engine_close(engine)                # free an OCR engine handle
PdfOxide.ocr_page_needs_ocr(doc, page)           # whether a page needs OCR
PdfOxide.ocr_extract_text(doc, page, engine \\ nil)  # OCR text (default or custom engine)

Document — 签名(读取与签署)

PdfOxide.sign(doc, certificate, reason \\ "", location \\ "")  # sign the document in place
PdfOxide.signature_count(doc)            # number of signatures
PdfOxide.signature(doc, index)           # {:ok, %SignatureInfo{}}
PdfOxide.verify_all_signatures(doc)      # verify every signature
PdfOxide.document_has_timestamp?(doc)    # whether the document is timestamped
PdfOxide.document_dss(doc)               # {:ok, %Dss{}} document security store
PdfOxide.convert_to_pdf_a(doc, level)    # convert the document to PDF/A (also accepts a %DocumentEditor{})

Document — 校验

PdfOxide.validate_pdf_a(doc, level)    # {:ok, %PdfAResult{}}
PdfOxide.validate_pdf_ua(doc, level)   # {:ok, %PdfUaResult{}}
PdfOxide.validate_pdf_x(doc, level)    # {:ok, %PdfXResult{}}

PdfOxide.pdf_a_compliant?(result)      # PDF/A compliance flag
PdfOxide.pdf_a_errors(result)          # list of PDF/A errors
PdfOxide.pdf_a_warning_count(result)   # PDF/A warning count
PdfOxide.pdf_a_close(result)           # free the result handle

PdfOxide.pdf_ua_accessible?(result)    # PDF/UA accessibility flag
PdfOxide.pdf_ua_errors(result)         # list of PDF/UA errors
PdfOxide.pdf_ua_warnings(result)       # list of PDF/UA warnings
PdfOxide.pdf_ua_stats(result)          # %UaStats{} element counts
PdfOxide.pdf_ua_close(result)          # free the result handle

PdfOxide.pdf_x_compliant?(result)      # PDF/X compliance flag
PdfOxide.pdf_x_errors(result)          # list of PDF/X errors
PdfOxide.pdf_x_close(result)           # free the result handle

DocumentEditor — 打开与信息

PdfOxide.open_editor(path)              # {:ok, %DocumentEditor{}}
PdfOxide.open_editor_from_bytes(bytes)  # open an editor from a binary
PdfOxide.editor_close(editor)           # free the editor handle

PdfOxide.editor_page_count(editor)      # number of pages
PdfOxide.editor_version(editor)         # %{major: _, minor: _}
PdfOxide.editor_modified?(editor)       # whether the editor has unsaved changes
PdfOxide.editor_source_path(editor)     # the editor's source path
PdfOxide.get_producer(editor)           # the /Producer metadata value
PdfOxide.set_producer(editor, value)    # set /Producer
PdfOxide.get_creation_date(editor)      # the /CreationDate value
PdfOxide.set_creation_date(editor, date)  # set /CreationDate

DocumentEditor — 页面与几何信息

PdfOxide.delete_page(editor, page_index)             # delete a page
PdfOxide.move_page(editor, from, to)                 # move a page
PdfOxide.rotate_page_by(editor, page, degrees)       # add to a page's rotation
PdfOxide.rotate_all_pages(editor, degrees)           # rotate all pages
PdfOxide.set_page_rotation(editor, page, degrees)    # set absolute rotation
PdfOxide.get_page_rotation(editor, page)             # get current rotation
PdfOxide.crop_margins(editor, left, right, top, bottom)  # crop all page margins
PdfOxide.get_page_crop_box(editor, page)             # get the CropBox as %Bbox{}
PdfOxide.set_page_crop_box(editor, page, x, y, w, h) # set the CropBox
PdfOxide.get_page_media_box(editor, page)            # get the MediaBox as %Bbox{}
PdfOxide.set_page_media_box(editor, page, x, y, w, h)  # set the MediaBox

DocumentEditor — 擦除与编辑性涂黑

PdfOxide.erase_region(editor, page, x, y, w, h)        # queue a rectangular erase
PdfOxide.erase_regions(editor, page, rects)            # queue several erase rectangles
PdfOxide.clear_erase_regions(editor, page)             # clear queued erases on a page
PdfOxide.apply_page_redactions(editor, page)           # apply queued redactions on a page
PdfOxide.apply_all_redactions(editor)                  # apply all queued redactions
PdfOxide.page_marked_for_redaction?(editor, page)      # whether a page has redactions queued
PdfOxide.unmark_page_for_redaction(editor, page)       # clear a page's redaction mark
PdfOxide.redaction_add(editor, page, x1, y1, x2, y2, r, g, b)  # queue a redaction rectangle with fill
PdfOxide.redaction_count(editor, page)                 # number of queued redactions on a page
PdfOxide.redaction_apply(editor, scrub_metadata \\ false, r \\ 0.0, g \\ 0.0, b \\ 0.0)  # apply redactions
PdfOxide.redaction_scrub_metadata(editor)              # strip Info/XMP/JS/embedded files only

DocumentEditor — 注释展平

PdfOxide.flatten_annotations(editor, page)         # flatten annotations on a page
PdfOxide.flatten_all_annotations(editor)           # flatten annotations document-wide
PdfOxide.page_marked_for_flatten?(editor, page)    # whether a page is marked for flatten
PdfOxide.unmark_page_for_flatten(editor, page)     # clear a page's flatten mark

DocumentEditor — 表单

PdfOxide.set_form_field_value(editor, name, value)   # set a form field value
PdfOxide.flatten_forms(editor)                       # flatten all form fields
PdfOxide.flatten_forms_on_page(editor, page_index)   # flatten forms on a page
PdfOxide.flatten_warnings_count(editor)              # number of flatten warnings
PdfOxide.flatten_warning(editor, index)              # one flatten warning message
PdfOxide.import_fdf_bytes(editor, bytes)             # import FDF form data bytes
PdfOxide.import_xfdf_bytes(editor, bytes)            # import XFDF form data bytes

DocumentEditor — 合并、嵌入与条形码

PdfOxide.merge_from(editor, source_path)             # merge pages from a PDF file
PdfOxide.merge_from_bytes(editor, bytes)             # merge pages from a PDF binary
PdfOxide.embed_file(editor, name, bytes)             # attach a file to the PDF
PdfOxide.extract_pages_to_bytes(editor, pages)       # extract a page subset to a new PDF binary
PdfOxide.add_barcode_to_page(editor, page, barcode, x, y, width, height)  # stamp a %Barcode{} on a page

DocumentEditor — 保存

PdfOxide.editor_save(editor, path)                                  # save to a file
PdfOxide.editor_save_to_bytes(editor)                               # serialize to a binary
PdfOxide.editor_save_to_bytes_with_options(editor, compress, garbage_collect, linearize)  # save with options
PdfOxide.editor_save_encrypted(editor, path, user_password, owner_password)  # save AES-256 encrypted to a file
PdfOxide.editor_save_encrypted_to_bytes(editor, user_password, owner_password)  # save encrypted to a binary

Pdf 创建(一步式)

PdfOxide.from_markdown(md)        # build a PDF from Markdown
PdfOxide.from_html(html)          # build a PDF from HTML
PdfOxide.from_text(text)          # build a PDF from plain text
PdfOxide.from_image(path)         # build a single-page PDF from an image file
PdfOxide.from_image_bytes(bytes)  # build a single-page PDF from image bytes
PdfOxide.from_html_css(html, css, font_bytes \\ <<>>)            # build from HTML + CSS + one font
PdfOxide.from_html_css_with_fonts(html, css, fonts)             # build from HTML + CSS + [{family, bytes}]
PdfOxide.merge(paths)             # merge several PDF files into one
PdfOxide.save(pdf, path)          # write a %Pdf{} or %RenderedImage{} to a path
PdfOxide.to_bytes(pdf)            # serialize a built %Pdf{} to a binary
PdfOxide.pdf_page_count(pdf)      # page count of a built %Pdf{}

DocumentBuilder

PdfOxide.builder()                                    # {:ok, %DocumentBuilder{}}
PdfOxide.builder_set_title(builder, title)            # set the title
PdfOxide.builder_set_author(builder, author)          # set the author
PdfOxide.builder_set_subject(builder, subject)        # set the subject
PdfOxide.builder_set_keywords(builder, keywords)      # set the keywords
PdfOxide.builder_set_creator(builder, creator)        # set the creator
PdfOxide.builder_on_open(builder, script)             # set a document-open JavaScript action
PdfOxide.builder_language(builder, lang)              # set the document language
PdfOxide.builder_tagged_pdf_ua1(builder)              # enable Tagged PDF / PDF/UA-1 output
PdfOxide.builder_role_map(builder, custom, standard)  # map a custom structure role to a standard one
PdfOxide.builder_register_embedded_font(builder, name, font)  # register an %EmbeddedFont{} (consumes it)
PdfOxide.builder_letter_page(builder)                 # start a US-Letter page → {:ok, %PageBuilder{}}
PdfOxide.builder_a4_page(builder)                     # start an A4 page → {:ok, %PageBuilder{}}
PdfOxide.builder_page(builder, width, height)         # start a custom-size page
PdfOxide.builder_build(builder)                       # build the PDF to a binary
PdfOxide.builder_save(builder, path)                  # save the built PDF to a file
PdfOxide.builder_save_encrypted(builder, path, user_password, owner_password)  # save encrypted to a file
PdfOxide.builder_to_bytes_encrypted(builder, user_password, owner_password)    # save encrypted to a binary
PdfOxide.builder_close(builder)                       # free the builder handle

PageBuilder — 文本与排版

PdfOxide.page_font(pb, name, size)         # set the active font and size
PdfOxide.page_at(pb, x, y)                 # move the cursor to (x, y)
PdfOxide.page_text(pb, text)               # draw text at the cursor
PdfOxide.page_heading(pb, level, text)     # draw a heading (level 1–6)
PdfOxide.page_paragraph(pb, text)          # draw a wrapped paragraph
PdfOxide.page_space(pb, points)            # advance vertical space
PdfOxide.page_horizontal_rule(pb)          # draw a horizontal rule
PdfOxide.page_columns(pb, column_count, gap_pt, text)  # flow text into columns
PdfOxide.page_footnote(pb, ref_mark, note_text)        # add a footnote
PdfOxide.page_newline(pb)                  # break to a new line
PdfOxide.page_inline(pb, text)             # append inline text
PdfOxide.page_inline_bold(pb, text)        # append inline bold text
PdfOxide.page_inline_italic(pb, text)      # append inline italic text
PdfOxide.page_inline_color(pb, r, g, b, text)  # append inline coloured text
PdfOxide.page_text_in_rect(pb, x, y, w, h, text, align)  # draw aligned text inside a rectangle
PdfOxide.page_new_page_same_size(pb)       # start a new page of the same size

PageBuilder — 链接与 JavaScript

PdfOxide.page_link_url(pb, url)             # add a URL link over the last element
PdfOxide.page_link_page(pb, page_index)     # add a link to another page
PdfOxide.page_link_named(pb, destination)   # add a named-destination link
PdfOxide.page_link_javascript(pb, script)   # add a JavaScript-action link
PdfOxide.page_on_open(pb, script)           # page-open JavaScript action
PdfOxide.page_on_close(pb, script)          # page-close JavaScript action
PdfOxide.page_field_keystroke(pb, script)   # field keystroke JavaScript
PdfOxide.page_field_format(pb, script)      # field format JavaScript
PdfOxide.page_field_validate(pb, script)    # field validate JavaScript
PdfOxide.page_field_calculate(pb, script)   # field calculate JavaScript

PageBuilder — 注释

PdfOxide.page_highlight(pb, r, g, b)            # highlight the last element
PdfOxide.page_underline(pb, r, g, b)            # underline the last element
PdfOxide.page_strikeout(pb, r, g, b)            # strike out the last element
PdfOxide.page_squiggly(pb, r, g, b)             # squiggly-underline the last element
PdfOxide.page_sticky_note(pb, text)             # add a sticky note at the cursor
PdfOxide.page_sticky_note_at(pb, x, y, text)    # add a sticky note at (x, y)
PdfOxide.page_watermark(pb, text)               # add a text watermark
PdfOxide.page_watermark_confidential(pb)        # add a CONFIDENTIAL watermark
PdfOxide.page_watermark_draft(pb)               # add a DRAFT watermark
PdfOxide.page_stamp(pb, type_name)              # add a rubber-stamp annotation
PdfOxide.page_freetext(pb, x, y, w, h, text)    # add a free-text annotation

PageBuilder — 表单控件

PdfOxide.page_text_field(pb, name, x, y, w, h, default_value \\ "")        # text field
PdfOxide.page_checkbox(pb, name, x, y, w, h, checked)                      # checkbox
PdfOxide.page_combo_box(pb, name, x, y, w, h, options, selected \\ "")     # combo box
PdfOxide.page_radio_group(pb, name, values, xs, ys, ws, hs, selected \\ "")  # radio group
PdfOxide.page_push_button(pb, name, x, y, w, h, caption)                   # push button
PdfOxide.page_signature_field(pb, name, x, y, w, h)                        # signature field

PageBuilder — 图形

PdfOxide.page_rect(pb, x, y, w, h)                       # stroke a rectangle outline
PdfOxide.page_filled_rect(pb, x, y, w, h, r, g, b)       # draw a filled rectangle
PdfOxide.page_line(pb, x1, y1, x2, y2)                   # draw a line
PdfOxide.page_stroke_rect(pb, x, y, w, h, width, r, g, b)  # stroke a rectangle with width/colour
PdfOxide.page_stroke_line(pb, x1, y1, x2, y2, width, r, g, b)  # stroke a line with width/colour
PdfOxide.page_stroke_rect_dashed(pb, x, y, w, h, width, r, g, b, dash, phase)  # dashed rectangle
PdfOxide.page_stroke_line_dashed(pb, x1, y1, x2, y2, width, r, g, b, dash, phase)  # dashed line

PageBuilder — 图像与条形码

PdfOxide.page_image(pb, bytes, x, y, w, h)                  # place an image from bytes
PdfOxide.page_image_with_alt(pb, bytes, x, y, w, h, alt_text)  # place an image with alt text
PdfOxide.page_image_artifact(pb, bytes, x, y, w, h)         # place an image marked as an artifact
PdfOxide.page_barcode_1d(pb, barcode_type, data, x, y, w, h)  # draw a 1-D barcode
PdfOxide.page_barcode_qr(pb, data, x, y, size)              # draw a QR code

PageBuilder — 表格

PdfOxide.page_table(pb, n_columns, widths, aligns, n_rows, cells, has_header)  # draw a static table
PdfOxide.page_streaming_table_begin(pb, n_columns, headers, widths, aligns, repeat_header)  # open a streaming table
PdfOxide.page_streaming_table_begin_v2(pb, n_columns, headers, widths, aligns, repeat_header, mode, sample_rows, min_w, max_w, max_rowspan)  # streaming table with width mode
PdfOxide.page_streaming_table_set_batch_size(pb, batch_size)   # set the auto-flush batch size
PdfOxide.page_streaming_table_pending_row_count(pb)            # rows buffered but not flushed
PdfOxide.page_streaming_table_batch_count(pb)                  # number of flushed batches
PdfOxide.page_streaming_table_push_row(pb, cells)              # push a row
PdfOxide.page_streaming_table_push_row_v2(pb, cells, rowspans) # push a row with per-cell rowspans
PdfOxide.page_streaming_table_flush(pb)                        # flush buffered rows
PdfOxide.page_streaming_table_finish(pb)                       # finish the streaming table

PageBuilder — 提交

PdfOxide.page_done(pb)   # commit the page to its parent builder (consumes the handle)
PdfOxide.page_close(pb)  # drop the page builder without committing

EmbeddedFont

PdfOxide.font_from_file(path)          # load a TTF/OTF font from a file
PdfOxide.font_from_bytes(bytes, name \\ "")  # load a font from bytes
PdfOxide.font_close(font)              # free the font handle

Certificate

PdfOxide.certificate_from_bytes(bytes, password \\ "")  # load a PKCS#12 certificate
PdfOxide.certificate_from_pem(cert_pem, key_pem)        # load a PEM certificate + key
PdfOxide.certificate_subject(cert)    # subject DN
PdfOxide.certificate_issuer(cert)     # issuer DN
PdfOxide.certificate_serial(cert)     # serial number
PdfOxide.certificate_validity(cert)   # validity window
PdfOxide.certificate_valid?(cert)     # whether the certificate is currently valid
PdfOxide.certificate_close(cert)      # free the certificate handle

Signing(直接对原始字节签名)

PdfOxide.sign_bytes(bytes, certificate, reason \\ "", location \\ "")  # sign raw PDF bytes
PdfOxide.sign_bytes_pades(bytes, certificate, level, tsa_url \\ "", opts \\ [])       # PAdES-sign raw bytes
PdfOxide.sign_bytes_pades_opts(bytes, certificate, level, tsa_url \\ "", opts \\ [])  # PAdES-sign via options struct
PdfOxide.add_timestamp(pdf_data, sig_index, tsa_url)  # add a document timestamp to a signature

sign_bytes_pades/5level0 = B-B,1 = B-T,2 = B-LT。opts 的键::reason:location:certs:crls:ocsps

SignatureInfo

PdfOxide.signature_signer_name(sig)        # the signer's name
PdfOxide.signature_reason(sig)             # the signing reason
PdfOxide.signature_location(sig)           # the signing location
PdfOxide.signature_time(sig)               # the signing time
PdfOxide.signature_certificate(sig)        # {:ok, %Certificate{}}
PdfOxide.signature_pades_level(sig)        # the PAdES level
PdfOxide.signature_has_timestamp?(sig)     # whether the signature is timestamped
PdfOxide.signature_timestamp(sig)          # {:ok, %Timestamp{}}
PdfOxide.signature_add_timestamp(sig, timestamp)  # attach a timestamp
PdfOxide.signature_verify(sig)             # verify the signature
PdfOxide.signature_verify_detached(sig, bytes)  # verify with a detached messageDigest check
PdfOxide.signature_close(sig)              # free the signature handle

Timestamp

PdfOxide.timestamp_parse(bytes)            # parse an RFC 3161 token → {:ok, %Timestamp{}}
PdfOxide.timestamp_token(ts)               # the raw token bytes
PdfOxide.timestamp_message_imprint(ts)     # the message imprint
PdfOxide.timestamp_time(ts)                # the timestamp time
PdfOxide.timestamp_serial(ts)              # the serial number
PdfOxide.timestamp_tsa_name(ts)            # the TSA name
PdfOxide.timestamp_policy_oid(ts)          # the policy OID
PdfOxide.timestamp_hash_algorithm(ts)      # the hash algorithm
PdfOxide.timestamp_verify(ts)              # verify the timestamp
PdfOxide.timestamp_close(ts)               # free the timestamp handle

TsaClient

PdfOxide.tsa_client(url, opts \\ [])                       # build a TSA client
PdfOxide.tsa_request_timestamp(client, data)              # request a timestamp over data
PdfOxide.tsa_request_timestamp_hash(client, hash, hash_algo)  # request a timestamp over a hash
PdfOxide.tsa_close(client)                                # free the TSA client handle

tsa_client/2opts 键::username:password:timeout:hash_algo:use_nonce:cert_req

Dss(文档安全存储)

PdfOxide.dss_cert_count(dss)    # number of certificates
PdfOxide.dss_crl_count(dss)     # number of CRLs
PdfOxide.dss_ocsp_count(dss)    # number of OCSP responses
PdfOxide.dss_vri_count(dss)     # number of VRI entries
PdfOxide.dss_cert(dss, index)   # one certificate (DER)
PdfOxide.dss_crl(dss, index)    # one CRL (DER)
PdfOxide.dss_ocsp(dss, index)   # one OCSP response (DER)
PdfOxide.dss_close(dss)         # free the DSS handle

Barcodes(独立条形码功能)

PdfOxide.generate_qr_code(data, error_correction \\ 1, size_px \\ 256)  # generate a QR code → %Barcode{}
PdfOxide.generate_barcode(data, format \\ 0, size_px \\ 256)            # generate a 1-D/2-D barcode
PdfOxide.barcode_data(barcode)          # the encoded data string
PdfOxide.barcode_format(barcode)        # the format code
PdfOxide.barcode_confidence(barcode)    # decode confidence (0.0–1.0)
PdfOxide.barcode_png(barcode, size_px \\ 256)  # render to PNG bytes
PdfOxide.barcode_svg(barcode, size_px \\ 256)  # render to an SVG string
PdfOxide.barcode_close(barcode)         # free the barcode handle

日志、配置与加密

PdfOxide.set_log_level(level)        # set the library log level
PdfOxide.get_log_level()             # get the current log level

PdfOxide.crypto_active_provider()    # the active crypto provider name
PdfOxide.crypto_cbom()               # the cryptographic bill of materials
PdfOxide.crypto_inventory()          # the crypto algorithm inventory
PdfOxide.crypto_policy()             # the current crypto policy
PdfOxide.crypto_fips_available()     # whether a FIPS provider is available
PdfOxide.crypto_use_fips()           # switch to the FIPS provider
PdfOxide.crypto_set_policy(spec)     # set the crypto policy from a spec string

PdfOxide.model_manifest()                  # the bundled OCR/model manifest
PdfOxide.prefetch_available()              # whether model prefetch is available
PdfOxide.prefetch_models(languages_csv \\ "")  # prefetch OCR models for given languages
PdfOxide.set_max_ops_per_stream(limit)         # cap content-stream operations per stream
PdfOxide.set_preserve_unmapped_glyphs(preserve)  # toggle preservation of unmapped glyphs

完整示例

# --- Extraction ---
{:ok, doc} = PdfOxide.open("input.pdf")
count = PdfOxide.page_count(doc)

text = PdfOxide.extract_text(doc, 0)
{:ok, chars} = PdfOxide.extract_chars(doc, 0)
{:ok, tables} = PdfOxide.extract_tables(doc, 0)

# Search
results = PdfOxide.search_all(doc, "configuration", false)

# --- Creation ---
{:ok, pdf} = PdfOxide.from_markdown("# Report\n\nGenerated by PDF Oxide.")
:ok = PdfOxide.save(pdf, "report.pdf")

# --- Builder ---
{:ok, b} = PdfOxide.builder()
PdfOxide.builder_set_title(b, "Invoice")
{:ok, pb} = PdfOxide.builder_a4_page(b)
PdfOxide.page_font(pb, "Helvetica", 12)
PdfOxide.page_at(pb, 72, 720)
PdfOxide.page_text(pb, "Hello, world")
PdfOxide.page_done(pb)
PdfOxide.builder_save(b, "invoice.pdf")

# --- Editing ---
{:ok, ed} = PdfOxide.open_editor("document.pdf")
PdfOxide.rotate_all_pages(ed, 90)
PdfOxide.set_form_field_value(ed, "name", "John Doe")
PdfOxide.editor_save(ed, "output.pdf")
PdfOxide.editor_close(ed)

Other Language Bindings

PDF Oxide 为所有主流生态系统提供原生绑定:Rust, Python, Node.js, WASM, C#, Golang, Java, PHP, Ruby, C++, Swift, Kotlin, Dart, R, Julia, Zig, Scala, Clojure, Objective-C

下一步