The font size appears significantly smaller than expected when using wkhtmltoimage to render

I am trying to convert text into an image, with a static layout and size while adjusting the font size based on the amount of text. I prefer using wkhtmltoimage 0.12.5 as it offers various CSS styling options. Currently, I am working on a Mac.

Below is a snippet from my Test.html file:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="flowtype.js"></script>
</head>
<body>
<div>Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test </div>
<script type="text/javascript">
$('body').flowtype();
</script>
</body>
</html>

You can find more information about Flowtype here.

Although this setup works in the browser, it does not render correctly through wkhtmltoimage.

When I use the command line below:

wkhtmltoimage --width 300 --height 100 Test.html Test.png

The text appears too small in the resulting image. Could this be related to the window size?

I have also tried using wkhtmltopdf to create a PDF, but encountered similar issues.

Any suggestions or ideas to resolve this problem?

Answer №1

If you are using `wkhtmltoimage` and the text appears too small, you can adjust the `fontRatio` setting to make it bigger. Another option is to increase the `--width` parameter to match a standard browser window size, for example, 1200px.

The issue here is that with a width of only 300px, FlowType aims for 45-75 characters per line by default. With each line being around 60 characters long, your characters end up being just 5px wide, hence the text looks tiny. Try resizing your browser window to 300px to see how the text matches what `wkhtmltoimage` produces.

To modify the font size basis in FlowType, use the `fontRatio` argument like this. Lower values will result in larger text:

$('body').flowtype({
  fontRatio: 15
});

The default value for `fontRatio` is 30, so experimenting with smaller values like 15 can help increase the text size.

For a practical demonstration, here is an example showcasing the impact of different `fontRatio` values on an element with a width of `300px`.

Screenshot:

https://i.sstatic.net/gRmxr.png

Code Snippet:

$('.c1').flowtype(); // Default, fontRatio = 30

$('.c2').flowtype({fontRatio: 15});
.c1, .c2 {
  width: 300px;
  border: solid red 2px;
  margin: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://fastcdn.org/FlowType.JS/1.1/flowtype.js"></script>

<div class="c1">Text Text Text Text Text Text Text Text Text Text Text Text Text Text</div>
<div class="c2">Text Text Text Text Text Text Text Text Text Text Text Text Text Text</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to use jQuery to delete specific table rows while keeping the first row safe from removal

Currently, I have a jQuery script that successfully removes table rows when a button is clicked. However, I want to prevent the button from removing the first row of the table. Is there a way to achieve this? $("#remove").click(function(event) { ...

Display movie details in a responsive column layout that condenses into a single column

I'm having trouble displaying movie information on a website due to CSS and HTML issues. My goal is to align the title (Director:, Cast:, etc) with the first item in the list, and have all subsequent items follow below. I initially tried using a defin ...

Navigating the ropes of push-pull functionality in Bootstrap 5

How can I utilize push and pull in Bootstrap version 5.0.0-beta2? In Bootstrap 3, my code looks like this: <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" /> <div class="row"> <d ...

A guide on retrieving and resetting the value of a radio button within a table

I need to create multiple radio buttons within a table using Razor syntax under ASP.NET Core MVC. Each row of the table should have an update and clear link to update the record and clear the selected radio button for that specific row. <table class=&qu ...

How can I streamline a kendo UI MVC project by eliminating unnecessary components?

After switching my MVC 5 project to utilize Kendo UI, I've noticed a significant increase in the number of files being used. Since there is no need for supporting other cultures at the moment, can I confidently delete the files within the messages an ...

After clicking in the Firefox browser, the Window.open function is unexpectedly showing [object Window]

I have added an anchor link that, when clicked, opens a new window with the specified link. Below is the code I used for this purpose: <a href="javascript:window.open('https://www.remax.fi/fi/kiinteistonvalitys/tietosuojaseloste/', &apos ...

NextJs getStaticPaths function is failing to render the correct page, resulting in a 404 error message being displayed

Hey there, I'm in a bit of a pickle as I've never used 'getStaticPaths' before and it's crucial for my current project! I followed the example code from NextJs's documentation on 'getStaticPaths', but when I try to ...

Next.js allows you to create a single page that corresponds to the root path '/' as well as a dynamic route '/param'

I have a single-page website built with Next.js. The home page, which displays a list of products, is located at route / and the corresponding code can be found in pages/index.js. Each product has an id, allowing users to jump directly to it using /#produc ...

Converting a base64 string to a PNG format for uploading to an S3 bucket from the frontend

Feeling a bit overwhelmed here, hoping this isn't just a repeat issue. I've come across similar problems but none of the solutions seem to be working for me at the moment. I'm currently utilizing react-signature-canvas for allowing users to ...

Change the rectangle's position by rotating another rectangle

I am facing a challenge with two rectangles that have pivots attached to them, The task at hand is to align the green rectangle's position based on the rotation of the red rectangle. The desired outcome should resemble the image provided: Despite t ...

Error Encountered When Updating cGridView in Yii: "TypeError: $.fn.yiiGridView is undefined"

I'm encountering an issue with updating the gridview TypeError: $.fn.yiiGridView is undefined; after using AjaxLink Click Button for Refresh <script> $(document).ready(function(){ $("#tombol_refresh").click(function(){ $.fn.yiiGridView ...

CSS - Overlapping elements on smaller screens due to being stacked

I am encountering an issue with a column of elements stacked below each other in my code. Here is a simplified version of my implementation: <div style="position: relative; height: 100%;"> <div style="position: absolute;">Really long text h ...

Adjusting input height with CSS in Google Chrome

When I define an input element with the following CSS properties: input { border: none; font-family: arial, sans-serif; font-size: 16px; line-height: 16px; padding: 0; } I anticipate the height of the input to be 16px, however, upon inspecting ...

Issues with location forecasts and .getPlace() when utilizing the Google Maps loader for Place Autocomplete in Vue are causing functionality problems

After setting up a Vue 2 app using the vue cli, I noticed that when typing an address, no suggestions were populating from the dropdown. Checking the network tab, I observed the AutocompletionService.GetPredictions call triggering without any errors upon p ...

Steps for resetting the ng-multiselect-dropdown

Is it necessary to wrap the ng-multiselect-dropdown in a form in order to reset it? code - app.component.html <div> <p id = "node">Select a Root API Object - </p> <p style="width:50%"> <ng-multiselect-dropdown [placeho ...

`The best way to access a JavaScript variable from PHP`

This is the code snippet I am working on: var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; document.form1.bookingsession.focus(); var coords = { "lat": daaa }; $.get('bs.php', coords, function () { ...

Issue with Masonry layout not adjusting to change in window size

Something seems to be fixed on displaying four rows, no matter the size of the window. Previously, it would adjust to three rows or fewer as the browser was resized. I recently played around with columnWidth, but reverting it back to 250 doesn't seem ...

Merging two distinct arrays of objects in JavaScript can be achieved by utilizing various methods and

I have a challenge where I need to merge two arrays of objects in a nested way. var array1=[{ PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2& ...

Oops! Issue: The mat-form-field is missing a MatFormFieldControl when referencing the API guide

I included the MatFormFieldModule in my code like so: import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; ...

Break up the JavaScript code into modules to avoid duplicating blocks of code detected by

There is a block of code that SONAR has identified as duplicate. I am looking for guidance on how to address this issue. import fields from '../../utils/utils'; dispatch( fields.change([ { id: 'userData', value: ...