Skip to content

Commit

Permalink
creat/mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Florents-Tselai committed Nov 6, 2024
1 parent d27b7e3 commit 2474c7e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ The following functions are available:
- `pdf_metadata(pdf) → text`
- `pdf_version(pdf) → text`
- `pdf_subject(pdf) → text`
- `pdf_creation(pdf) → timestamp`
- `pdf_modification(pdf) → timestamp`

Below are some examples

Expand Down
69 changes: 68 additions & 1 deletion pgpdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/jsonb.h"
#include "utils/datetime.h"
#include "utils/date.h"

#include "poppler.h"
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

Expand Down Expand Up @@ -255,6 +257,71 @@ pdf_page(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(cstring_to_text(poppler_page_get_text(page)));
}

PG_FUNCTION_INFO_V1(pdf_creation);

Datum
pdf_creation(PG_FUNCTION_ARGS)
{
PopplerDocument *doc = PG_GETARG_POPPLER_DOCUMENT(0);
GDateTime *dt = poppler_document_get_creation_date_time(doc);

if (dt == NULL)
PG_RETURN_NULL();

gint year = g_date_time_get_year(dt);
gint month = g_date_time_get_month(dt);
gint day = g_date_time_get_day_of_month(dt);
gint hour = g_date_time_get_hour(dt);
gint minute = g_date_time_get_minute(dt);
gint second = g_date_time_get_second(dt);

g_date_time_unref(dt);

TimestampTz ts = DatumGetTimestamp(DirectFunctionCall6(
make_timestamp,
Int32GetDatum(year),
Int32GetDatum(month),
Int32GetDatum(day),
Int32GetDatum(hour),
Int32GetDatum(minute),
Float8GetDatum((double)second)
));

PG_RETURN_TIMESTAMPTZ(ts);
}

PG_FUNCTION_INFO_V1(pdf_modification);

Datum
pdf_modification(PG_FUNCTION_ARGS)
{
PopplerDocument *doc = PG_GETARG_POPPLER_DOCUMENT(0);
GDateTime *dt = poppler_document_get_modification_date_time(doc);

if (dt == NULL)
PG_RETURN_NULL();

gint year = g_date_time_get_year(dt);
gint month = g_date_time_get_month(dt);
gint day = g_date_time_get_day_of_month(dt);
gint hour = g_date_time_get_hour(dt);
gint minute = g_date_time_get_minute(dt);
gint second = g_date_time_get_second(dt);

g_date_time_unref(dt);

TimestampTz ts = DatumGetTimestamp(DirectFunctionCall6(
make_timestamp,
Int32GetDatum(year),
Int32GetDatum(month),
Int32GetDatum(day),
Int32GetDatum(hour),
Int32GetDatum(minute),
Float8GetDatum((double)second)
));

PG_RETURN_TIMESTAMPTZ(ts);
}

PG_FUNCTION_INFO_V1(pdf_from_bytea);

Expand Down
12 changes: 12 additions & 0 deletions sql/pgpdf--0.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ CREATE FUNCTION pdf_subject(pdf) RETURNS TEXT
AS
'MODULE_PATHNAME';

CREATE FUNCTION pdf_creation(pdf) RETURNS TIMESTAMP
IMMUTABLE
STRICT
LANGUAGE C
AS
'MODULE_PATHNAME';

CREATE FUNCTION pdf_modification(pdf) RETURNS TIMESTAMP
IMMUTABLE
STRICT
LANGUAGE C
AS
'MODULE_PATHNAME';


CREATE FUNCTION bytea_to_pdf(bytea) RETURNS pdf
Expand Down
12 changes: 12 additions & 0 deletions test/expected/pgpdf.out
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ SELECT pdf_subject('/tmp/pgintro.pdf');

(1 row)

SELECT pdf_creation('/tmp/pgintro.pdf');
pdf_creation
--------------------------
Wed Jul 20 11:13:37 2011
(1 row)

SELECT pdf_modification('/tmp/pgintro.pdf');
pdf_modification
--------------------------
Wed Jul 20 11:13:37 2011
(1 row)

/* bytea -> pdf */
SELECT pg_read_binary_file('/tmp/pgintro.pdf')::pdf::text = '/tmp/pgintro.pdf'::pdf::text;
?column?
Expand Down
3 changes: 2 additions & 1 deletion test/sql/pgpdf.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ SELECT pdf_keywords('/tmp/pgintro.pdf');
SELECT pdf_metadata('/tmp/pgintro.pdf');
SELECT pdf_version('/tmp/pgintro.pdf');
SELECT pdf_subject('/tmp/pgintro.pdf');

SELECT pdf_creation('/tmp/pgintro.pdf');
SELECT pdf_modification('/tmp/pgintro.pdf');

/* bytea -> pdf */
SELECT pg_read_binary_file('/tmp/pgintro.pdf')::pdf::text = '/tmp/pgintro.pdf'::pdf::text;
Expand Down

0 comments on commit 2474c7e

Please sign in to comment.