ACF Library

testArticleLayout

Back to List

Description: A test function to generate PDF Article from its parameter. An image container, title, ingress, body text and optional output path.

Call options

FileMaker calculation

Set Variable [$res; ACF_Run("testArticleLayout"; container_im;  string_title;  string_ingress;  string_body;  string_outputPath)]

Category: EXAMPLES

Dependencies

  • GetDirectoriesFromPath: Remove the filename part from a full path, and return the directory path NOTE: put USE bootstrap; below the package header instead of copying the code for GetDirectoriesFromPath

Function source:

// Footer JSON builder. 
function ftr ()
    return JSONarray(
        JSON("op", "setStrokeColor", "color", JSONarray(0.72, 0.72, 0.72)),
        JSON("op", "line", "x1", 50, "y1", 0, "x2", 545, "y2", 0),
        JSON("op", "setFillColor", "color", JSONarray(0.35, 0.35, 0.35)),
        JSON("op", "text", "x", 50, "y", 24, "size", 9, "value", "Generated by ACFPDF"),
        JSON("op", "text", "x", 50, "y", 24, "width", 495, "size", 9,
            "align", "right", "value", "Page {page} of {pages}")
    ); 
end

// 
// Main function....

function testArticleLayout (container im, string title, string ingress, string body, string outputPath)
    if (outputPath == "") then
        outputPath = documents_directory() + "test/sampleOutput.pdf";
    end if
    string res; 
    string dir = GetDirectoriesFromPath (outputPath); 
    if (!directory_exists(dir)) then
        res = create_directory(dir); 
    end if

    // Start generating the article
    // Picture centered on top - keep aspect ratio. get_imageSize(im) returns 1530x1024 to example. 
    // Title centered below the image "h1 style"
    // Ingress in bold style below the title, one column. Left justified. 
    // The body of the article below the Ingress, two columns. Balanced columns. Gutter 5 mm. Stretch justified. 

    set_locale("en_US.UTF8"); 
    int pdf = pdf_create(outputPath);
    pdf_setPageOptions(pdf, JSON(
        "pageSize", "A4",
        "author", "ACF smoke test",
        "title", title
    ));
    pdf_setPageFooter(pdf, JSON("commands", ftr()), "all");
    pdf_setFont(pdf, JSON("name", "Arial", "embed", true));

    int imageId = pdf_addImage(pdf, im);
    string imageSize = get_ImageSize(im);
    array string imageParts = explode("x", imageSize);
    float sourceWidth = float(imageParts[1]);
    float sourceHeight = float(imageParts[2]);
    if (sourceWidth <= 0 || sourceHeight <= 0) then
        sourceWidth = 1530;
        sourceHeight = 1024;
    end if

    float pageLeft = 50;
    float contentWidth = 495;
    float imageWidth = contentWidth;
    float imageHeight = imageWidth * sourceHeight / sourceWidth;
    if (imageHeight > 250) then
        imageHeight = 250;
        imageWidth = imageHeight * sourceWidth / sourceHeight;
    end if
    float imageX = pageLeft + (contentWidth - imageWidth) / 2;

    pdf_placeImage(pdf, imageId, imageX, 44, imageWidth, imageHeight, JSON("padding", 0));

    float titleY = 66 + imageHeight + 24;
    pdf_drawText(pdf, title, JSON(
        "x", pageLeft,
        "y", titleY,
        "width", contentWidth,
        "size", 24,
        "lineHeight", 29,
        "align", "center"
    ));

    float ingressY = titleY + 22;
    pdf_drawTextBox(pdf, ingress, pageLeft, ingressY, contentWidth, 70, JSON(
        "size", 12,
        "lineHeight", 16,
        "align", "stretch"
    ));

    float bodyY = ingressY + 50;
    pdf_drawTextBox(pdf, body, pageLeft, bodyY, contentWidth, 300, JSON(
        "size", 10.5,
        "lineHeight", 14,
        "align", "stretch",
        "paragraphSpacing", 7,
        "columns", 2,
        "gutter", 14,
        "balanceColumns", true
    ));

    repeat
        string rest = pdf_GetUnplacedRest(pdf);
        if (rest != "") then
            pdf_newPage(pdf);
            pdf_drawText(pdf, title + " - continued", JSON(
                "x", pageLeft,
                "width", contentWidth,
                "size", 16,
                "lineHeight", 20,
                "align", "left"
            ));
            pdf_drawTextBox(pdf, rest, pageLeft, 120, contentWidth, 560, JSON(
                "size", 10.5,
                "lineHeight", 14,
                "align", "stretch",
                "columns", 2,
                "gutter", 14
            ));
        end if
    until (rest == ""); 

    pdf_newPage(pdf);
    pdf_drawText(pdf, "Overflow buffer smoke test", JSON(
        "x", pageLeft,
        "width", contentWidth,
        "size", 16,
        "lineHeight", 20,
        "align", "left"
    ));
    pdf_drawTextBox(pdf,
        "Overflow smoke test: this intentionally small box should leave text in the unplaced buffer so the new function can be tested. The rest of this sentence should not fit in the box.",
        pageLeft, 130, contentWidth, 18,
        JSON("size", 9, "lineHeight", 11, "align", "stretch"));
    rest = pdf_GetUnplacedRest(pdf);
    if (rest != "") then
        pdf_drawText(pdf, "Unplaced rest captured: " + left(rest, 90), JSON(
            "x", pageLeft,
            "y", 164,
            "width", contentWidth,
            "size", 8,
            "lineHeight", 10,
            "align", "left"
        ));
    end if

    close_pdf(pdf);


    return outputPath; 
end

The testArticleLayout

This is a demonstration of teh new DPF-writer functions in the ACF language.

The function produces, to example this layout.

sampleOutput

Back to List