Skip to content

Commit

Permalink
Added button to save labels as png images
Browse files Browse the repository at this point in the history
Resolves #80
Resolves #348
  • Loading branch information
Donkie committed Aug 7, 2024
1 parent 7082c52 commit 10ad8ce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"antd": "^5.20.0",
"axios": "^1.7.3",
"flag-icons": "^7.2.3",
"html-to-image": "^1.11.11",
"i18next": "^23.12.2",
"i18next-browser-languagedetector": "^8.0.0",
"i18next-http-backend": "^2.5.2",
Expand Down
3 changes: 2 additions & 1 deletion client/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"deleteSettings": "Delete Current Preset",
"deleteSettingsConfirm": "Are you sure you want to delete this preset?",
"settingsName": "Preset Name",
"saveSetting": "Save Presets"
"saveSetting": "Save Presets",
"saveAsImage": "Save as Image"
},
"qrcode": {
"button": "Print Labels",
Expand Down
31 changes: 30 additions & 1 deletion client/src/pages/printing/printingDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrinterOutlined } from "@ant-design/icons";
import { FileImageOutlined, PrinterOutlined } from "@ant-design/icons";
import { useTranslate } from "@refinedev/core";
import {
Button,
Expand All @@ -14,6 +14,7 @@ import {
Slider,
Space,
} from "antd";
import * as htmlToImage from "html-to-image";
import React, { useRef } from "react";
import ReactToPrint from "react-to-print";
import { useSavedState } from "../../utils/saveload";
Expand Down Expand Up @@ -119,6 +120,7 @@ const PrintingDialog: React.FC<PrintingDialogProps> = ({
return (
<div
key={itemIdx}
className="print-page-item"
style={{
width: `${itemWidth}mm`,
height: `${itemHeight}mm`,
Expand Down Expand Up @@ -175,6 +177,30 @@ const PrintingDialog: React.FC<PrintingDialogProps> = ({
);
});

const saveAsImage = () => {
const hasPrinted: Element[] = [];

Array.from(document.getElementsByClassName("print-qrcode-item")).forEach(async (item) => {
// Prevent printing copies
for (let i = 0; i < hasPrinted.length; i += 1) {
if (item.isEqualNode(hasPrinted[i])) return;
}
hasPrinted.push(item);

// Generate png image
const url = await htmlToImage.toPng(item as HTMLElement, {
backgroundColor: "#FFF",
cacheBust: true,
});

// Download image
const link = document.createElement("a");
link.href = url;
link.download = "spoolmanlabel.png";
link.click();
});
};

return (
<>
<Row gutter={16}>
Expand Down Expand Up @@ -788,6 +814,9 @@ const PrintingDialog: React.FC<PrintingDialogProps> = ({
<Col>
<Space>
{extraButtons}
<Button type="primary" icon={<FileImageOutlined />} size="large" onClick={saveAsImage}>
{t("printing.generic.saveAsImage")}
</Button>
<ReactToPrint
key="print-button"
trigger={() => (
Expand Down

0 comments on commit 10ad8ce

Please sign in to comment.