Giter Site home page Giter Site logo

Comments (6)

yGuy avatar yGuy commented on September 26, 2024 1

your "svgElement" is not an SvgElement, but rather a document.

Read the whole answer in the linked stackoverflow question and add .documentElement to your parser call.

Regarding the react stuff - svgElement2 also isn't an SVGElement, but an object that only makes sense to React.

Please use the Discussions to discuss issues in your code. If you find a bug in the library, create a test-case and open a new issue. Thanks.

from svg2pdf.js.

yGuy avatar yGuy commented on September 26, 2024

Why?

This lib needs to work on the SVG DOM. Why should it also deal with parsing and loading? What's the use-case?

from svg2pdf.js.

insinfo avatar insinfo commented on September 26, 2024

my use case is to convert a layout/designer made with fabric.js to PDF in the browser, it would make it much easier if this lib already handles this

image

from svg2pdf.js.

yGuy avatar yGuy commented on September 26, 2024

Much easier?

Much easier than this: https://stackoverflow.com/a/24109000 ?

from svg2pdf.js.

HackbrettXXX avatar HackbrettXXX commented on September 26, 2024

I'm also against adding this to the library. We could add it as a code snippet to the readme.

from svg2pdf.js.

mdodge-ecgrow avatar mdodge-ecgrow commented on September 26, 2024

I'm trying to add an SVG to a new PDF using this method. Basically when the user clicks a link it is supposed to pop up a modal with the newly created pdf in the modal. I am getting this error: Uncaught (in promise) TypeError: e2.tagName is undefined.

Here is the relevant code in my modal component:

...
const svgLogoData = `<svg xmlns="http://www.w3.org/2000/svg" width="400" height="89.474"><g fill-rule="evenodd"><path fill...</svg>`;
...
const PriceListPdf = (props: PriceListPDFProps) => {
...
useEffect(() => {
		console.log({priceListToExport});
		if (priceListToExport) {
			displayPDF();

		}
	}, [priceListToExport]);

const displayPDF = () => {
		const doc = new jsPDF(
			{
				orientation: 'p',
				unit: 'in',
				format: 'letter',
				putOnlyUsedFonts: true,
				floatPrecision: 16 // or "smart", default is 16
			});

		const left = doc.internal.pageSize.width / 2;
		// const top = doc.internal.pageSize.height / 2;

		const imgWidth = 4;
		const imgLeft = left - (imgWidth / 2);

		const parser = new DOMParser();
		const svgElement = parser.parseFromString(svgLogoData, "image/svg+xml");
		console.log({svgElement});

		doc.svg(svgElement, {x: imgLeft, y: 0.1, width: imgWidth, height: .889}).then(() => {
			doc.setDisplayMode('fullheight');
			const objectURL = doc.output('datauristring');
			setPDFSrc(objectURL)
		});
};
...

Also tried this:

import logo from '../../assets/logo.svg';
...
const svgElement2 = <img src={logo} alt="Logo"/>;
doc.svg(svgElement2, {x: imgLeft, y: 0.1, width: imgWidth, height: .889}).then(() => {
	doc.setDisplayMode('fullheight');
	const objectURL = doc.output('datauristring');
	setPDFSrc(objectURL)
});

That second method throws this in the console: Uncaught (in promise) TypeError: this.rootSvg.querySelectorAll is not a function

from svg2pdf.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.