简体   繁体   中英

How to set dynamic page size (auto) in XSL-FO using Apache FOP?

I want to set the page size that should be dynamic according to the page contents.

That is, I want the page size of the PDF should be just as per the content size and not like A4, letter or something else.

My code is:

<?xml version="1.0" encoding="UTF-8"?>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"  xmlns="http://www.w3.org/1998/Math/MathML">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page">
            <fo:region-body margin="1in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="my-page">
         <fo:flow flow-name="xsl-region-body" font="12pt Times">
            <fo:block font-size="1.5em" font-weight="bold" role="html:p">
                <fo:instream-foreign-object>
                    <math display="inline">
                        <mrow>
                            <mi>A</mi>
                            <mo>=</mo>
                            <mi>Ï€</mi>
                            <msup>
                                <mi>r</mi>
                                <mn>2</mn>
                            </msup>
                        </mrow>
                    </math>
                </fo:instream-foreign-object>
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

What I get is:

在此处输入图片说明

What I need is:

在此处输入图片说明

You could probably accomplish what you wish by changing the process slightly. JEuclid has an "foprep" function that would convert the MathML inside of XSL FO into the inline SVG for you. You could then apply an identity-translate on the resulting XSL FO to use the dimensions of the SVG as the dimensions of the page.

I did not do a complete test, but foprep on your sample would yield something like this (note I changed a few things like removing any margin on the original page and also set line-height on the block to "0" so that an exact size image fits.

This is what is the result of running "foprep" on your document.

 <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns="http://www.w3.org/1998/Math/MathML">
<fo:layout-master-set>
    <fo:simple-page-master master-name="my-page">
        <fo:region-body/>
    </fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
    <fo:flow flow-name="xsl-region-body" font="12pt Times">
        <fo:block line-height="0">
            <fo:instream-foreign-object alignment-adjust="-9.090909%">
                <svg xmlns="http://www.w3.org/2000/svg"
                    xmlns:xlink="http://www.w3.org/1999/xlink" color-interpolation="auto"
                    color-rendering="auto" contentScriptType="text/ecmascript"
                    contentStyleType="text/css" fill="black" fill-opacity="1"
                    font-family="'Dialog'" font-size="12" font-style="normal"
                    font-weight="normal" height="11" image-rendering="auto"
                    preserveAspectRatio="xMidYMid meet" shape-rendering="auto" stroke="black"
                    stroke-dasharray="none" stroke-dashoffset="0" stroke-linecap="square"
                    stroke-linejoin="miter" stroke-miterlimit="10" stroke-opacity="1"
                    stroke-width="1" text-rendering="auto" version="1.0" width="39"
                    zoomAndPan="magnify">
                    <!--Converted from MathML using JEuclid-->
                    <defs id="genericDefs"/>
                    <g>
                        <g color-interpolation="linearRGB" color-rendering="optimizeQuality"
                            image-rendering="optimizeQuality"
                            text-rendering="optimizeLegibility" transform="translate(0.5469,10)">
                            <path
                                d="M3.9688 0 L4.0312 -0.2812 Q4.2969 -0.3125 4.4453 -0.4375 Q4.5938 -0.5625 4.6562 -0.7969 Q4.7188 -1.0312 4.7188 -1.4062 Q4.7188 -1.625 4.7031 -1.9062 L4.6719 -2.6562 L2.0938 -2.6562 L1.7656 -2.0781 Q1.5312 -1.6719 1.4297 -1.3828 Q1.3281 -1.0938 1.3281 -0.875 Q1.3281 -0.375 1.9219 -0.2812 L1.8594 0 L-0.5469 0 L-0.4844 -0.2812 Q-0.2969 -0.3125 -0.1328 -0.4297 Q0.0312 -0.5469 0.2109 -0.7891 Q0.3906 -1.0312 0.7188 -1.5938 L4.4688 -8.0938 L5.3594 -8.0938 L5.7812 -1.5625 Q5.8125 -1.0938 5.8828 -0.8359 Q5.9531 -0.5781 6.0859 -0.4531 Q6.2188 -0.3281 6.4688 -0.2812 L6.4062 0 L3.9688 0 ZM2.3438 -3.1719 L4.6406 -3.1719 L4.5469 -5.0469 Q4.5312 -5.4375 4.5156 -6.0156 Q4.5 -6.5938 4.5 -6.8594 L4.4375 -6.8594 L2.3438 -3.1719 Z"
                                stroke="none"/>
                        </g>
                        <g color-interpolation="linearRGB" color-rendering="optimizeQuality"
                            image-rendering="optimizeQuality"
                            text-rendering="optimizeLegibility" transform="translate(9.0775,10)">
                            <path
                                d="M0.7344 -4.2344 L0.7344 -5.0469 L5.9219 -5.0469 L5.9219 -4.2344 L0.7344 -4.2344 ZM0.7344 -1.8125 L0.7344 -2.625 L5.9219 -2.625 L5.9219 -1.8125 L0.7344 -1.8125 Z"
                                stroke="none"/>
                        </g>
                        <g color-interpolation="linearRGB" color-rendering="optimizeQuality"
                            image-rendering="optimizeQuality"
                            text-rendering="optimizeLegibility"
                            transform="translate(17.7957,10)">
                            <path
                                d="M2.5 -1.4688 Q2.5 -1.0781 2.5234 -0.8906 Q2.5469 -0.7031 2.6094 -0.6016 Q2.6719 -0.5 2.7891 -0.4297 Q2.9062 -0.3594 3.1875 -0.2812 L3.1875 0 L0.7031 0 L0.7031 -0.2812 Q1.0781 -0.375 1.1953 -0.4922 Q1.3125 -0.6094 1.3516 -0.8203 Q1.3906 -1.0312 1.3906 -1.4688 L1.3906 -6.5312 Q1.3906 -6.9375 1.3672 -7.125 Q1.3438 -7.3125 1.2734 -7.4141 Q1.2031 -7.5156 1.0938 -7.5781 Q0.9844 -7.6406 0.7031 -7.7188 L0.7031 -8 L3.1875 -8 L3.1875 -7.7188 Q2.9219 -7.6406 2.8125 -7.5781 Q2.7031 -7.5156 2.6328 -7.4219 Q2.5625 -7.3281 2.5312 -7.1328 Q2.5 -6.9375 2.5 -6.5312 L2.5 -1.4688 ZM3.5176 -9.9062 L3.5176 -8.875 L2.5332 -8.875 L2.5332 -9.9062 L3.5176 -9.9062 ZM1.3457 -9.9062 L1.3457 -8.875 L0.377 -8.875 L0.377 -9.9062 L1.3457 -9.9062 ZM6.7344 -3.0781 Q6.7812 -2.5 6.9141 -2.0156 Q7.0469 -1.5312 7.2812 -1.1797 Q7.5156 -0.8281 7.8438 -0.625 Q8.1719 -0.4219 8.625 -0.4219 Q9.1094 -0.4219 9.4141 -0.5234 Q9.7188 -0.625 9.9219 -0.8516 Q10.125 -1.0781 10.3125 -1.5312 L10.8906 -1.5312 L10.8906 -0.2031 Q10.3594 -0.0625 9.7891 0.0156 Q9.2188 0.0938 8.5312 0.0938 Q7.2188 0.0938 6.4844 -0.7109 Q5.75 -1.5156 5.5938 -3.0781 L4.2031 -3.0781 L4.2031 -3.6406 L5.5625 -3.6406 L5.5625 -3.9375 Q5.5625 -4.2969 5.5938 -4.6719 L4.625 -4.6719 L4.625 -5.2344 L5.6875 -5.2344 Q5.9688 -6.6094 6.8125 -7.3516 Q7.6562 -8.0938 8.9062 -8.0938 Q9.4375 -8.0938 9.8828 -8.0391 Q10.3281 -7.9844 10.8906 -7.8281 L10.8906 -6.5 L10.3125 -6.5 Q10.1719 -6.9062 9.9531 -7.1406 Q9.7344 -7.375 9.4531 -7.4766 Q9.1719 -7.5781 8.7656 -7.5781 Q8.2812 -7.5781 7.8672 -7.3047 Q7.4531 -7.0312 7.1719 -6.5078 Q6.8906 -5.9844 6.7812 -5.2344 L10.3281 -5.2344 L10.3281 -4.6719 L6.7344 -4.6719 Q6.7031 -4.4062 6.7031 -4.0156 L6.7031 -3.8438 Q6.7031 -3.7344 6.7031 -3.6406 L9.9219 -3.6406 L9.9219 -3.0781 L6.7344 -3.0781 Z"
                                stroke="none"/>
                        </g>
                        <g color-interpolation="linearRGB" color-rendering="optimizeQuality"
                            image-rendering="optimizeQuality"
                            text-rendering="optimizeLegibility"
                            transform="translate(28.6863,10)">
                            <path
                                d="M2.3281 -5.6562 L2.6875 -5.6562 L2.2812 -4.2188 L2.3438 -4.1875 Q2.9062 -4.9531 3.4375 -5.3281 Q3.9688 -5.7031 4.5312 -5.7031 Q4.9531 -5.7031 5.2344 -5.625 L4.9531 -4.3125 L4.3594 -4.3125 Q4.3125 -4.625 4.2109 -4.7812 Q4.1094 -4.9375 3.8906 -4.9375 Q3.6719 -4.9375 3.375 -4.7031 Q3.0781 -4.4688 2.7656 -4.0625 Q2.4531 -3.6562 2.2969 -3.3047 Q2.1406 -2.9531 2.0625 -2.5312 L1.5312 0 L0.5156 0 L1.4062 -4 Q1.5312 -4.5469 1.5312 -4.8438 Q1.5312 -5 1.4766 -5.0938 Q1.4219 -5.1875 1.3047 -5.2344 Q1.1875 -5.2812 0.9062 -5.2969 L0.9688 -5.6094 L2.3281 -5.6562 Z"
                                stroke="none"/>
                        </g>
                        <g color-interpolation="linearRGB" color-rendering="optimizeQuality"
                            image-rendering="optimizeQuality"
                            text-rendering="optimizeLegibility"
                            transform="translate(33.9207,7.3281)">
                            <path
                                d="M3.0781 -0.7031 Q3.2969 -0.7031 3.4297 -0.7266 Q3.5625 -0.75 3.6406 -0.8125 Q3.7188 -0.875 3.7656 -0.9766 Q3.8125 -1.0781 3.8906 -1.2812 L4.2656 -1.2812 L4.1875 0 L0.5625 0 L0.5625 -0.2188 Q0.75 -0.6719 1.0703 -1.1562 Q1.3906 -1.6406 1.9844 -2.2969 Q2.5 -2.8594 2.7188 -3.1406 Q2.9688 -3.4688 3.0859 -3.6953 Q3.2031 -3.9219 3.2578 -4.125 Q3.3125 -4.3281 3.3125 -4.5469 Q3.3125 -4.875 3.2109 -5.125 Q3.1094 -5.375 2.8906 -5.5234 Q2.6719 -5.6719 2.3438 -5.6719 Q1.5156 -5.6719 1.2344 -4.75 L0.7031 -4.75 L0.7031 -5.5938 Q1.2344 -5.8438 1.6953 -5.9531 Q2.1562 -6.0625 2.5312 -6.0625 Q3.3438 -6.0625 3.7578 -5.7109 Q4.1719 -5.3594 4.1719 -4.6719 Q4.1719 -4.4375 4.1328 -4.2422 Q4.0938 -4.0469 3.9922 -3.8594 Q3.8906 -3.6719 3.7266 -3.4453 Q3.5625 -3.2188 3.3594 -2.9922 Q3.1562 -2.7656 2.6562 -2.25 Q1.8125 -1.3594 1.4219 -0.7031 L3.0781 -0.7031 Z"
                                stroke="none"/>
                        </g>
                    </g>
                </svg>
            </fo:instream-foreign-object>
        </fo:block>
    </fo:flow>
</fo:page-sequence>
</fo:root>

Now, if I apply an XSL to that which just copies everything and grabs the width and height of the SVG to use as the page-width and page-height.

This XSL would copy dimensions for the SVG and use them as page-width, page-height in the FO:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="svg" version="1.0">
    <xsl:template match="fo:simple-page-master">
        <fo:simple-page-master>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="page-width">
                <xsl:value-of select="concat(//*[namespace-uri()='http://www.w3.org/2000/svg' and local-name()='svg'][1]/@width,'px')"/>
            </xsl:attribute>
            <xsl:attribute name="page-height">
                <xsl:value-of select="concat(//*[namespace-uri()='http://www.w3.org/2000/svg' and local-name()='svg'][1]/@height,'px')"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </fo:simple-page-master>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I would get this output in the page template (abbreviated only showing the page-master):

 <fo:simple-page-master master-name="my-page" page-width="39px" page-height="11px">
        <fo:region-body/>
    </fo:simple-page-master>

As you can see, the page-width and page-height is set to the dimensions of the SVG.

This process chain would allow you to the format that to a page that is the size of the SVG (as calculated by JEuclid).

The result is this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM