Position of the image when converting HTML to PDF

I’m working on Certificate level 2 and facing a challenge with positioning an image on PDF.
I was able to put the receipt and image together in the PDF but the position of the image is not centered (slightly on the right side from the center).
The same thing is happening on all images.

I tried both <center> tag and <style> tag (currently commented out) but neither of them worked.

Following is the code (related part):

Order robots from RobotSpareBin Industries Inc
    Open the robot order website
    ${orders}=    Get orders
    FOR    ${row}    IN    @{orders}
        Close the annoying modal
        Populate One Order    ${row}
        ${pdf}=    Set Variable    ${OUTPUT_DIR}${/}SalesReceipt${/}receipt_${row}[Order number].pdf
        ${receipt_html}=    Store the receipt as a HTML    ${row}[Order number]
        ${screenshot}=    Take a screenshot of the robot    ${row}[Order number]
        Embed the robot screenshot to the receipt PDF file    ${screenshot}    ${receipt_html}    ${pdf}
        Go to order another robot
    END
    Create a ZIP file of the receipts

Store the receipt as a HTML
    [Arguments]    ${order_number}
    ${order_receipt}=    Get Element Attribute    id:receipt    outerHTML
    [Return]    ${order_receipt}

Take a screenshot of the robot
    [Arguments]    ${order_number}
    Wait Until Page Contains Element    id:robot-preview-image    timeout=${GLOBAL_WAIT_TIMEOUT}
    ${path_screenshot}    Set Variable    ${OUTPUT_DIR}${/}Screenshots${/}screenshot_${order_number}.png
    Screenshot    id:robot-preview-image    ${path_screenshot}
    [Return]    ${path_screenshot}

Embed the robot screenshot to the receipt PDF file
    [Arguments]    ${screenshot}    ${receipt_html}    ${pdf}
    Html To Pdf
    ...    ${receipt_html}<br><br><center><img src='${screenshot}' height='200'/></center>
    # ...    ${receipt_html}<br><br><style> img {display: block; margin-left: auto; margin-right: auto} </style><img src='${screenshot}' height='200'/>
    ...    ${pdf}

Hi, given this example, you can observe that styling doesn’t always work as expected, but at least you can use Add Watermark Image To Pdf keyword for adding aligned images. (and HTML code isn’t required with this keyword)


How the PDF should be rendered:

What we got with both the HTML img tag render and the added watermark of the same image:


→ as you can see, the latter is better

1 Like

Hi Cosmin,
Thank you so much for your reply. This solution using “Add Watermark Image To Pdf” worked perfectly!

Koki

1 Like