Skip to content

Instantly share code, notes, and snippets.

@paul-snively
Created June 2, 2023 19:58
Show Gist options
  • Save paul-snively/69833c2605aa09a328caec2c3865fa53 to your computer and use it in GitHub Desktop.
Save paul-snively/69833c2605aa09a328caec2c3865fa53 to your computer and use it in GitHub Desktop.
XSLT to transform SVG converted from PDF so it scales in an HTML5 img tag, per <https://css-tricks.com/scale-svg/>.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns='http://www.w3.org/2000/svg'
xmlns:svg='http://www.w3.org/2000/svg'
exclude-result-prefixes="xs"
version="1.1">
<xsl:template match="svg:svg[@viewBox]">
<xsl:copy>
<xsl:copy-of select="@*[name(.)!='height' and name(.) != 'width']" />
<xsl:attribute name="height">100%</xsl:attribute>
<xsl:attribute name="preserveAspectRatio">xMidYMid meet</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<!-- IdentityTransform -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment