This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(document): repair pdf with libreoffic
- Loading branch information
1 parent
162ddad
commit 656d902
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from io import BytesIO | ||
import json | ||
import base64 | ||
import sys | ||
|
||
# TODO: Deal with the import error when running the code in the docker container | ||
# from pdf_to_markdown import PDFTransformer | ||
|
||
if __name__ == "__main__": | ||
json_str = sys.stdin.buffer.read().decode('utf-8') | ||
params = json.loads(json_str) | ||
pdf_string = params["PDF"] | ||
|
||
decoded_bytes = base64.b64decode(pdf_string) | ||
pdf_file_obj = BytesIO(decoded_bytes) | ||
pdf = PDFTransformer(x=pdf_file_obj) | ||
pages = pdf.raw_pages | ||
output = { | ||
"required": len(pages) == 0, | ||
} | ||
print(json.dumps(output)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package document | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/instill-ai/component/base" | ||
"github.com/instill-ai/component/internal/util" | ||
) | ||
|
||
func RequiredToRepair(pdfBase64 string) bool { | ||
|
||
paramsJSON := map[string]interface{}{ | ||
"PDF": base.TrimBase64Mime(pdfBase64), | ||
} | ||
|
||
pythonCode := pdfTransformer + pdfChecker | ||
|
||
outputBytes, err := util.ExecutePythonCode(pythonCode, paramsJSON) | ||
|
||
if err != nil { | ||
// It shouldn't block the original process. | ||
log.Println("failed to run python script: %w", err) | ||
return false | ||
} | ||
|
||
var output struct { | ||
Repair bool `json:"required"` | ||
} | ||
|
||
err = json.Unmarshal(outputBytes, &output) | ||
|
||
if err != nil { | ||
// It shouldn't block the original process. | ||
log.Println("failed to unmarshal output: %w", err) | ||
} | ||
|
||
return output.Repair | ||
} | ||
|
||
func RepairPDF(pdfBase64 string) (string, error) { | ||
return ConvertToPDF(pdfBase64, "pdf") | ||
} |
This file contains 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
This file contains 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