专栏名称: GitHubStore
分享有意思的开源项目
目录
相关文章推荐
Linux就该这么学  ·  开源装机工具 Ventoy 更新 ... ·  昨天  
Linux就该这么学  ·  告别繁琐操作!Linux ... ·  2 天前  
Linux就该这么学  ·  Fedora即将登陆Win10/11 ... ·  3 天前  
Linux爱好者  ·  嵌入式开发实战:国产8nm AIoT全流程 ·  4 天前  
51好读  ›  专栏  ›  GitHubStore

PostgreSQL的PDF数据类型扩展pgpdf

GitHubStore  · 公众号  ·  · 2024-11-23 07:50

正文

项目简介

PostgreSQL 的这个扩展提供了 pdf 数据类型和各种函数。


您可以通过转换 text 路径或 bytea 列来创建 pdf 类型。

SELECT '/tmp/pgintro.pdf'::pdf;
                                       pdf                                        ---------------------------------------------------------------------------------- PostgreSQL Introduction                                                         + Digoal.Zhou                                                                     + 7/20/2011Catalog                                                                +  PostgreSQL Origin

如果您的文件系统中没有 PDF 文件,但已将其内容存储在 bytea 列中,则可以将其转换为 pdf


为什么? :这允许您以符合 ACID 的方式处理 PDF。通常的替代方案依赖于外部脚本或服务,这很容易使您的数据摄取管道变得脆弱并使原始数据不同步。


实际的 PDF 解析是由poppler完成的。


用法

下载一些 PDF。

wget https://wiki.postgresql.org/images/e/ea/PostgreSQL_Introduction.pdf -O /tmp/pgintro.pdfwget https://pdfobject.com/pdf/sample.pdf -O /tmp/sample.pdf

创建一个包含 pdf 列的表:

CREATE TABLE pdfs(name text primary key, doc pdf);
INSERT INTO pdfs VALUES ('pgintro', '/tmp/pgintro.pdf');INSERT INTO pdfs VALUES ('pgintro', '/tmp/sample.pdf');

解析和验证应该自动发生。文件只会从磁盘读取一次!

Note 笔记

文件路径应该可以被 postgres 进程/用户访问!这与运行 psql 的用户不同。如果您作为 DBA 不明白这意味着什么!

字符串函数和运算符

标准 Postgres字符串函数和运算符应该照常工作:

SELECT 'Below is the PDF we received ' || '/tmp/pgintro.pdf'::pdf;
SELECT upper('/tmp/pgintro.pdf'::pdf::text);
SELECT nameFROM pdfsWHERE doc::text LIKE '%Postgres%';

全文搜索 (FTS)

您还可以执行全文搜索 (FTS),因为您可以像处理普通文本一样处理 pdf 文件。

SELECT '/tmp/pgintro.pdf'::pdf::text @@ to_tsquery('postgres');
 ?column? ---------- t(1 row)
SELECT '/tmp/pgintro.pdf'::pdf::text @@ to_tsquery('oracle');
 ?column? ---------- f(1 row)

pg_trgm 的文档相似度

您可以使用pg_trgm来获取两个文档之间的相似度:


元数据

可以使用以下功能:

  • pdf_title(pdf) → text

  • pdf_author(pdf) → text

  • pdf_num_pages(pdf) → integer


    文档的总页数

  • pdf_page(pdf, integer) → text


    获取第 i 页作为文本

  • pdf_creator(pdf) → text

  • pdf_keywords(pdf) → text

  • pdf_metadata(pdf) → text

  • pdf_version(pdf) → text

  • pdf_subject(pdf) → text

  • pdf_creation(pdf) → timestamp

  • pdf_modification(pdf) → timestamp

SELECT pdf_title('/tmp/pgintro.pdf');
        pdf_title






请到「今天看啥」查看全文