-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added image to pdf tool
- Loading branch information
Showing
27 changed files
with
693 additions
and
169 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,5 @@ yarn-error.log* | |
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
.contentlayer | ||
.contentlayer | ||
search-meta.json |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<!-- TODO: create release only when a new tag is released --> | ||
<!-- TODO: add seo --> | ||
<!-- wasm notes --> | ||
|
||
``` | ||
|
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,125 @@ | ||
"use client"; | ||
|
||
import FileUploader from "@/components/file-uploader/file-uploader"; | ||
import {useFileUploaderStore} from "@/components/file-uploader/store"; | ||
import {subtitle, title} from "@/components/primitives"; | ||
import {getToolByHref} from "@/config/tools"; | ||
import {MimeType} from "@/libs/mime"; | ||
import {downloadFile} from "@/utils/download"; | ||
import { | ||
Button, | ||
Modal, | ||
ModalBody, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
Spacer, | ||
useDisclosure, | ||
} from "@nextui-org/react"; | ||
import {usePathname} from "next/navigation"; | ||
import {useEffect, useState} from "react"; | ||
import {WorkerInput, WorkerOutput} from "./worker"; | ||
|
||
const allowedFileTypes: MimeType[] = [ | ||
"image/jpeg", | ||
"image/webp", | ||
"image/png", | ||
// TODO: add support for these too | ||
// "image/svg+xml", | ||
// "image/bmp", | ||
// "image/tiff", | ||
// "image/gif", | ||
// "image/heif", | ||
// "image/heic", | ||
]; | ||
|
||
export default function page() { | ||
const {files, reset, error} = useFileUploaderStore(); | ||
const path = usePathname(); | ||
const tool = getToolByHref(path); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const {isOpen, onOpen, onOpenChange} = useDisclosure(); | ||
|
||
function _startProcess() { | ||
setIsLoading(true); | ||
|
||
const worker = new Worker(new URL("./worker.ts", import.meta.url)); | ||
worker.onmessage = (event: MessageEvent<WorkerOutput>) => { | ||
setIsLoading(false); | ||
const {blob, error} = event.data; | ||
if (error?.length == 0) { | ||
downloadFile(blob, files[0].name.split(".")[0] + "-merged.pdf"); | ||
} else { | ||
onOpen(); | ||
reset(); | ||
} | ||
}; | ||
|
||
const workerInput: WorkerInput = { | ||
files: files, | ||
}; | ||
worker.postMessage(workerInput); | ||
} | ||
|
||
useEffect(() => { | ||
if (error.length > 0) { | ||
onOpen(); | ||
reset(); | ||
} | ||
}, [error]); | ||
|
||
return ( | ||
<> | ||
<center> | ||
<Spacer y={3} /> | ||
<h1 className={title({color: "green"})}>{tool?.title}</h1> | ||
<h2 | ||
className={subtitle({ | ||
fullWidth: true, | ||
})} | ||
> | ||
{tool?.description} | ||
</h2> | ||
<Spacer y={6} /> | ||
<FileUploader primaryColor="#18c964" acceptedFileTypes={allowedFileTypes} /> | ||
<Spacer y={6} /> | ||
{files.length > 0 ? ( | ||
<div className="grid grid-cols-2 gap-2"> | ||
<Button color="danger" variant="bordered" onPress={reset}> | ||
Reset | ||
</Button> | ||
<Button | ||
color="success" | ||
variant="bordered" | ||
isLoading={isLoading} | ||
onPress={_startProcess} | ||
> | ||
Convert to PDF | ||
</Button> | ||
</div> | ||
) : null} | ||
</center> | ||
<Modal backdrop="blur" isOpen={isOpen} onOpenChange={onOpenChange}> | ||
<ModalContent> | ||
{(onClose) => ( | ||
<> | ||
<ModalHeader className="flex flex-col gap-1">Invalid File</ModalHeader> | ||
<ModalBody> | ||
<p> | ||
One or more of the files you have selected are not supported, invalid, or | ||
corrupted. | ||
</p> | ||
<p>Please ensure that the file is valid and not corrupted.</p> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="danger" variant="flat" onPress={onClose}> | ||
OK | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
)} | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
} |
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,48 @@ | ||
import {jsPDF} from "jspdf"; | ||
|
||
export interface WorkerInput { | ||
files: File[]; | ||
} | ||
|
||
export interface WorkerOutput { | ||
blob: Blob; | ||
error: string[]; | ||
} | ||
|
||
const onmessage = (input: MessageEvent<WorkerInput>) => { | ||
const {files} = input.data; | ||
|
||
async function _start() { | ||
try { | ||
const pdfDoc = new jsPDF(); | ||
pdfDoc.deletePage(1); | ||
for (const file of files) { | ||
var img = URL.createObjectURL(file); | ||
var imgProps = pdfDoc.getImageProperties(img); | ||
var page = pdfDoc.addPage( | ||
[imgProps.height, imgProps.width], | ||
imgProps.height > imgProps.width ? "portrait" : "landscape", | ||
); | ||
page.addImage(img, imgProps.fileType, 0, 0, imgProps.width, imgProps.height); | ||
URL.revokeObjectURL(img); | ||
} | ||
|
||
var workerOutput: WorkerOutput = { | ||
blob: pdfDoc.output("blob"), | ||
error: [], | ||
}; | ||
postMessage(workerOutput); | ||
} catch (error) { | ||
var workerOutput: WorkerOutput = { | ||
blob: new Blob(), | ||
error: ["An error occurred while processing the images. Please try again."], | ||
}; | ||
postMessage(workerOutput); | ||
console.error("🍎 Error: ", error); | ||
} | ||
} | ||
|
||
_start(); | ||
}; | ||
|
||
addEventListener("message", onmessage); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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
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.