Created
June 5, 2023 12:44
-
-
Save iamucil/4ef25b1ee8a8a763337c88a655bb8c2e to your computer and use it in GitHub Desktop.
Extract metadata from pdf file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pdflib | |
import ( | |
"fmt" | |
"time" | |
"github.com/timemore/foundation/errors" | |
pdf "github.com/unidoc/unipdf/v3/model" | |
) | |
func Export() { | |
fmt.Println("hello") | |
} | |
type PDFDocMetaDataInfo struct { | |
Title string | |
NumPages int64 | |
Author string | |
Subject string | |
Keywords string | |
Creator string | |
Producer string | |
CreationDate *time.Time | |
ModifiedDate *time.Time | |
Trapped string | |
CustomInfo map[string]string | |
} | |
func MetaData(r *pdf.PdfReader) (*PDFDocMetaDataInfo, error) { | |
numPages, err := r.GetNumPages() | |
if err != nil { | |
return nil, errors.Wrap("get document pages", err) | |
} | |
documentMetaData := PDFDocMetaDataInfo{ | |
NumPages: int64(numPages), | |
} | |
if pdfInfo, err := r.GetPdfInfo(); err == nil { | |
if pdfInfo.Title != nil { | |
documentMetaData.Title = pdfInfo.Title.Decoded() | |
} | |
if pdfInfo.Author != nil { | |
documentMetaData.Author = pdfInfo.Author.Decoded() | |
} | |
if pdfInfo.Subject != nil { | |
documentMetaData.Subject = pdfInfo.Subject.Decoded() | |
} | |
if pdfInfo.Keywords != nil { | |
documentMetaData.Keywords = pdfInfo.Keywords.Decoded() | |
} | |
if pdfInfo.Creator != nil { | |
documentMetaData.Creator = pdfInfo.Creator.Decoded() | |
} | |
if pdfInfo.Producer != nil { | |
documentMetaData.Producer = pdfInfo.Producer.Decoded() | |
} | |
if pdfInfo.CreationDate != nil { | |
creationDate := pdfInfo.CreationDate.ToGoTime() | |
documentMetaData.CreationDate = &creationDate | |
} | |
if pdfInfo.ModifiedDate != nil { | |
modifiedDate := pdfInfo.ModifiedDate.ToGoTime() | |
documentMetaData.ModifiedDate = &modifiedDate | |
} | |
if pdfInfo.Trapped != nil { | |
documentMetaData.Trapped = pdfInfo.Trapped.String() | |
} | |
customInfoKeys := pdfInfo.CustomKeys() | |
documentMetaData.CustomInfo = make(map[string]string, len(customInfoKeys)) | |
for _, key := range customInfoKeys { | |
documentMetaData.CustomInfo[key] = pdfInfo.GetCustomInfo(key).Decoded() | |
} | |
} | |
return &documentMetaData, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment