Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm having "Word found unreadable content" message while adding images to docx. #2952

Open
alperkizilcay opened this issue Jan 31, 2025 · 0 comments

Comments

@alperkizilcay
Copy link

alperkizilcay commented Jan 31, 2025

Hello everyone!

I'm working on React.js. After adding image to docx, I'm having "Word found unreadable content" message. I reviewed many help materials but could not find a solution.

My code is here.

import { Document, Packer, Paragraph, ImageRun } from "docx";
import { saveAs } from "file-saver";

const App = () => {
  const [imageData, setImageData] = useState(null);

  const handleFileChange = (event) => {
    const file = event.target.files[0];
    if (file) {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onloadend = () => {
        setImageData(reader.result.split(",")[1]);
      };
    }
  };

  const generateDocx = async () => {

    const doc = new Document({
      sections: [
        {
          children: [
            new Paragraph({
              children: [
                new ImageRun({
                  data: Uint8Array.from(atob(imageData), (c) =>
                    c.charCodeAt(0)
                  ),
                  transformation: {
                    width: 200,
                    height: 100,
                  },
                }),
              ],
            }),
          ],
        },
      ],
    });

    const blob = await Packer.toBlob(doc);
    saveAs(blob, "document.docx");
  };

  return (
    <div>
      <input type="file" accept="image/*" onChange={handleFileChange} />
      <button onClick={generateDocx}>DOCX İndir</button>
    </div>
  );
};

export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant