This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(document): repair pdf with libreoffic #374
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chuang8511
I think this PR is not ready, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will deal with this part with other PR.
But, this PR is not ready because there is a bug in the container for prod. I may need to modify this PR. So, I draft it first.