[Python] Using Tailwindcss with weasyprint in python

Discussão em 'Python' iniciado por Stack, Agosto 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use tailwindcss with weasyprint in python to generate PDFs but it doesn't apply any of the classes. Not sure what I'm doing wrong.

    Very basic template:

    templates/basic.html

    <!DOCTYPE html>
    <html>

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>

    <body>
    <h1 class="text-3xl font-bold underline">Hello world!</h1>
    <div class="foo">This is a Foo</div>
    </body>
    </html>


    with this CSS:

    assets/basic.css

    @import "tailwindcss";

    .foo {
    font-size: 12px;
    }


    I know I need to compile the CSS and use the compiled version with weasyprint, to i do this in my Dockerfile

    FROM node:20-alpine AS node-builder

    WORKDIR /tmp
    RUN npm install tailwindcss @tailwindcss/cli

    FROM node-builder AS tailwind-base

    COPY --from=node-builder /tmp/node_modules /tmp/node_modules
    COPY --from=backend-base /app/assets/*.css /tmp/
    COPY --from=backend-base /app/templates/ /tmp/templates/

    WORKDIR /tmp
    RUN npx @tailwindcss/cli -i ./basic.css -o ./basic.compiled.css


    I then copy the compiled css into my app stage

    COPY --from=tailwind-base /tmp/basic.compiled.css /app/css/basic.compiled.css

    and use it with weasyprint:

    css = CSS("/app/css/basic.compiled.css")
    html = HTML(string=self.to_html(), base_url="/app/")
    virtual_file = BytesIO()
    html.write_pdf(target=virtual_file, stylesheets=[css])


    the resulting compiled CSS:

    /*! tailwindcss v4.1.12 | MIT License | https://tailwindcss.com */
    @layer properties;
    @layer theme, base, components, utilities;
    @layer theme {
    :root, :host {
    --font-sans: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji",
    "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
    "Courier New", monospace;
    --text-xs: 0.75rem;
    --text-xs--line-height: calc(1 / 0.75);
    --text-xl: 1.25rem;
    --text-xl--line-height: calc(1.75 / 1.25);
    --text-3xl: 1.875rem;
    --text-3xl--line-height: calc(2.25 / 1.875);
    --font-weight-bold: 700;
    --default-font-family: var(--font-sans);
    --default-mono-font-family: var(--font-mono);
    }
    }
    @layer base {
    *, ::after, ::before, ::backdrop, ::file-selector-button {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0 solid;
    }
    html, :host {
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
    tab-size: 4;
    font-family: var(--default-font-family, ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");
    font-feature-settings: var(--default-font-feature-settings, normal);
    font-variation-settings: var(--default-font-variation-settings, normal);
    -webkit-tap-highlight-color: transparent;
    }
    hr {
    height: 0;
    color: inherit;
    border-top-width: 1px;
    }
    abbr:where([title]) {
    -webkit-text-decoration: underline dotted;
    text-decoration: underline dotted;
    }
    h1, h2, h3, h4, h5, h6 {
    font-size: inherit;
    font-weight: inherit;
    }
    a {
    color: inherit;
    -webkit-text-decoration: inherit;
    text-decoration: inherit;
    }
    b, strong {
    font-weight: bolder;
    }
    code, kbd, samp, pre {
    font-family: var(--default-mono-font-family, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace);
    font-feature-settings: var(--default-mono-font-feature-settings, normal);
    font-variation-settings: var(--default-mono-font-variation-settings, normal);
    font-size: 1em;
    }
    small {
    font-size: 80%;
    }
    sub, sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
    }
    sub {
    bottom: -0.25em;
    }
    sup {
    top: -0.5em;
    }
    table {
    text-indent: 0;
    border-color: inherit;
    border-collapse: collapse;
    }
    :-moz-focusring {
    outline: auto;
    }
    progress {
    vertical-align: baseline;
    }
    summary {
    display: list-item;
    }
    ol, ul, menu {
    list-style: none;
    }
    img, svg, video, canvas, audio, iframe, embed, object {
    display: block;
    vertical-align: middle;
    }
    img, video {
    max-width: 100%;
    height: auto;
    }
    button, input, select, optgroup, textarea, ::file-selector-button {
    font: inherit;
    font-feature-settings: inherit;
    font-variation-settings: inherit;
    letter-spacing: inherit;
    color: inherit;
    border-radius: 0;
    background-color: transparent;
    opacity: 1;
    }
    :where(select:is([multiple], [size])) optgroup {
    font-weight: bolder;
    }
    :where(select:is([multiple], [size])) optgroup option {
    padding-inline-start: 20px;
    }
    ::file-selector-button {
    margin-inline-end: 4px;
    }
    ::placeholder {
    opacity: 1;
    }
    @supports (not (-webkit-appearance: -apple-pay-button)) or (contain-intrinsic-size: 1px) {
    ::placeholder {
    color: currentcolor;
    @supports (color: color-mix(in lab, red, red)) {
    color: color-mix(in oklab, currentcolor 50%, transparent);
    }
    }
    }
    textarea {
    resize: vertical;
    }
    ::-webkit-search-decoration {
    -webkit-appearance: none;
    }
    ::-webkit-date-and-time-value {
    min-height: 1lh;
    text-align: inherit;
    }
    ::-webkit-datetime-edit {
    display: inline-flex;
    }
    ::-webkit-datetime-edit-fields-wrapper {
    padding: 0;
    }
    ::-webkit-datetime-edit, ::-webkit-datetime-edit-year-field, ::-webkit-datetime-edit-month-field, ::-webkit-datetime-edit-day-field, ::-webkit-datetime-edit-hour-field, ::-webkit-datetime-edit-minute-field, ::-webkit-datetime-edit-second-field, ::-webkit-datetime-edit-millisecond-field, ::-webkit-datetime-edit-meridiem-field {
    padding-block: 0;
    }
    ::-webkit-calendar-picker-indicator {
    line-height: 1;
    }
    :-moz-ui-invalid {
    box-shadow: none;
    }
    button, input:where([type="button"], [type="reset"], [type="submit"]), ::file-selector-button {
    appearance: button;
    }
    ::-webkit-inner-spin-button, ::-webkit-outer-spin-button {
    height: auto;
    }
    [hidden]:where:)not([hidden="until-found"])) {
    display: none !important;
    }
    }
    @layer utilities {
    .text-3xl {
    font-size: var(--text-3xl);
    line-height: var(--tw-leading, var(--text-3xl--line-height));
    }
    .text-xl {
    font-size: var(--text-xl);
    line-height: var(--tw-leading, var(--text-xl--line-height));
    }
    .text-xs {
    font-size: var(--text-xs);
    line-height: var(--tw-leading, var(--text-xs--line-height));
    }
    .font-bold {
    --tw-font-weight: var(--font-weight-bold);
    font-weight: var(--font-weight-bold);
    }
    .underline {
    text-decoration-line: underline;
    }
    }
    .foo {
    font-size: 6px;
    }
    @property --tw-font-weight {
    syntax: "*";
    inherits: false;
    }
    @layer properties {
    @supports ((-webkit-hyphens: none) and (not (margin-trim: inline))) or ((-moz-orient: inline) and (not (color:rgb(from red r g b)))) {
    *, ::before, ::after, ::backdrop {
    --tw-font-weight: initial;
    }
    }
    }


    The .foo works but not the H1. I copied that from the get started guide on tailwindcss docs.

    tailwindcss version v4.1.12 weasyprint version v66.0

    Continue reading...

Compartilhe esta Página