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

Embed images with SVG #287

Open
Leo-Nicolle opened this issue May 16, 2024 · 5 comments
Open

Embed images with SVG #287

Leo-Nicolle opened this issue May 16, 2024 · 5 comments
Labels
bug help wanted Help with creating a proper test-case, looking up the spec, or creating a pull request.

Comments

@Leo-Nicolle
Copy link

Describe the bug
tags with an href pointing to a SVG file are not displayed correctly

What version are you using (exact version of and jspdf)?
- jspdf: 2.5.1
- svg2pdf.js: 2.2.3

To Reproduce

  1. Test SVG:
<svg
      width="128"
      height="64"
      version="1.1"
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      style="user-select: none"
    >
      <rect x="0" y="0" width="128" height="64" stroke="black" fill="none" />
      <image
        width="64"
        height="64"
        x="0"
        y="0"
        href="https://upload.wikimedia.org/wikipedia/commons/1/16/Cellular_phone.svg"
      />
      <image
        width="64"
        height="64"
        x="64"
        y="0"
        href="https://upload.wikimedia.org/wikipedia/commons/a/a0/Goldfish-47022_1280.png"
      />
</svg>

Code:

import { jsPDF } from "jspdf";
import "svg2pdf.js";
const svg = document.querySelector("svg");
const doc = new jsPDF({ format: "a4", unit: "pt" });
doc.svg(svg, { x: 0, y: 0, width: 128, height: 64 }).then((doc) => {
  doc.save("doc.pdf");
});
  1. I get a square with the PNG image but not the SVG

Example Sandbox

Expected behavior
The SVG image should be drawn

Screenshots
Input SVG:
Screenshot from 2024-05-16 15-27-24
Output PDF:
Screenshot from 2024-05-16 15-27-08

Desktop (please complete the following information):

  • OS: Ubuntu
  • Browser Chromium
  • Version 125.0.6422.26
@HackbrettXXX
Copy link
Member

Thank you for the bug report. It appears the SVG image is part of the exported PDF but is at the wrong location. Could you provide an example with an image tag linking a much simpler SVG that still reproduces the issue?

@Leo-Nicolle
Copy link
Author

Sure ! Here it is !

@HackbrettXXX
Copy link
Member

Thanks. I could not reproduce the issue with the sandbox, though. There is a bug in your code:

const svg = document.querySelector("svg");

should be

const svg = document.querySelector("#toexport");

Otherwise, we're just exporting the first SVG. After I fixed it, the exported PDF looks fine.

@Leo-Nicolle
Copy link
Author

Hi ! Sorry for the mistake, I might have rushed the second example a little.
So I rewinded the tape, copy/pasted the cellphone SVG, and added a shape on the top left corner of it, and TADAA! Both SVG are actually exported, they are just not at the same scale as in the browser.
So here is a (working + reproducing the bug) example

On the browser

The SVG to export scales down the second SVG so that it fits within the view
Screenshot from 2024-09-03 13-49-54

On the exported PDF:

Both SVGs are rendered in their original scale, and cropped within the view.
Screenshot from 2024-09-03 13-49-42

@HackbrettXXX
Copy link
Member

Thank you. I can reproduce it now. It seems like this is an issue with the viewbox transformation being applied incorrectly. If I add a viewBox attribute to the referenced image, the result is correct. I suspect the fallback case here is wrong:

const x = this.getX(context)
const y = this.getY(context)
const viewBox = this.getViewBox()
let nodeTransform
if (viewBox) {
const width = this.getWidth(context)
const height = this.getHeight(context)
nodeTransform = computeViewBoxTransform(this.element, viewBox, x, y, width, height, context)
} else {
nodeTransform = context.pdf.Matrix(1, 0, 0, 1, x, y)
}
return nodeTransform

I'm not sure what the expected behavior is if no viewBox attribute is provided. I couldn't find much on a quick scan through the SVG spec.

I'll accept a pull request for this issue. A workaround for now might be to always set the viewBox attribute.

@HackbrettXXX HackbrettXXX added bug help wanted Help with creating a proper test-case, looking up the spec, or creating a pull request. labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted Help with creating a proper test-case, looking up the spec, or creating a pull request.
Projects
None yet
Development

No branches or pull requests

2 participants