The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →In jsPDF, align an image by calculating the x and y coordinates you pass to addImage, along with its target width and height. For horizontal centering inside a box, use x = left + (boxWidth - imageWidth) / 2. The inspected API declaration does not show an image align option, so alignment is a layout calculation rather than a built-in flag.
How image placement works in jsPDF
The addImage method places an image using its data, format, x-coordinate, y-coordinate, width, and height. The current jsPDF TypeScript declaration also lists optional alias, compression, and rotation arguments. The basic example uses explicit coordinates and dimensions.
Think of those dimensions as the image rectangle. To align it, first decide the rectangle or region it should align with: the page, a content area bounded by margins, or another element. Then calculate the top-left coordinate of the image rectangle. The API declaration inspected does not define a named image alignment flag such as align: "center" or align: "right".
Choose a consistent unit and alignment box
Set the document’s base unit when creating the jsPDF instance, then use that same unit for page measurements, margins, image dimensions, and coordinates. The constructor documentation lists points, millimeters, centimeters, inches, pixels, picas, em, and ex; millimeters are the default, and A4 is the default page format. These defaults can be changed by constructor options.
#1 Best Overall
- INNOVATIVE CARTRIDGE-FREE PRINTING — No more dealing with lots of tiny ink cartridges; With this wireless document and photo printer each ink bottle set is equivalent to about 90 individual cartridges²
- LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; When you choose this combination printer, scanner and copier you can print up to 4,500 pages black/7,500 color³
- COLOR PRINTING — Up to 2 years of ink in the box4 (and with every replacement ink set) for fewer out-of-ink frustrations
- ZERO CARTRIDGE WASTE — By using an Epson EcoTank printer you can help reduce the amount of cartridge waste ending up in landfills
- HOME PRINTER DESIGNED FOR RELIABILITY — The Epson EcoTank ET-2800 All-in-One Supertank Color Printer creates vivid, detailed prints and documents thanks to Micro Piezo Heat-Free Technology; Fire off 10 ISO pages per minute1 to easily finish large jobs
For page-level alignment, the box may be the whole page. For a layout with margins, use the content area instead. If the left and right margins differ, the content area’s left edge and width differ from a simple centered-page calculation. Keep both values explicit.
- Page box: left edge is 0; width is the page width.
- Content box: left edge is the left margin; width is page width minus left and right margins.
- Custom box: use the box’s own left, top, width, and height, in the document’s base unit.
In current projects, check the installed jsPDF version for the page-size methods you use. The current declarations expose internal page-size methods, but public APIs can evolve; do not assume a method from a different project version without checking it.
Calculate left, center, right, and vertical alignment
Let left be the box’s left coordinate, top its top coordinate, and boxWidth and boxHeight its dimensions. Let imageWidth and imageHeight be the dimensions you plan to pass to addImage.
| Desired placement | Coordinate calculation |
|---|---|
| Left aligned | x = left |
| Horizontally centered | x = left + (boxWidth - imageWidth) / 2 |
| Right aligned | x = left + boxWidth - imageWidth |
| Vertically centered | y = top + (boxHeight - imageHeight) / 2 |
For centering in both axes, calculate both centered coordinates. These formulas position the image rectangle; they do not inspect the pixels inside the file.
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #2
- CARTRIDGE-FREE PRINTING — Print lab-quality photos, graphics and creative projects; Get vibrant colors and sharp text with Epson's high-accuracy printhead and Claria ET Premium 6-color inks
- INK BOTTLES — Save on photos1 and creative projects with affordable in-house printing; All-in-one printer allows you to print 4" x 6" photos for about 4 cents each vs. 40 cents with traditional ink cartridges1
- LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; Printer, scanner and copier lets you print up to 6,200 color pages³
- PRINT FOR LONGER — Up to 2 years of ink in the box² (and with every replacement ink set) for fewer out-of-ink frustrations with this wireless printer
- ZERO CARTRIDGE WASTE — Epson EcoTank printer helps reduce the amount of cartridge waste ending up in landfills; Cartridge-free printer uses high-yield ink bottles; Each replacement ink bottle set is equivalent to about 100 individual ink cartridges⁴
Center an image on a page with margins
This example centers an 80-by-50-unit PNG horizontally in the usable content width and places its top edge 40 units from the top. It uses millimeters explicitly so the margin and image dimensions share the document’s base unit. Supply imageData in a form accepted by the jsPDF version in your project.
import { jsPDF } from "jspdf";
const doc = new jsPDF({ unit: "mm", format: "a4" });
const pageWidth = doc.internal.pageSize.getWidth();
const marginLeft = 15;
const marginRight = 15;
const imageWidth = 80;
const imageHeight = 50;
const contentWidth = pageWidth - marginLeft - marginRight;
const x = marginLeft + (contentWidth - imageWidth) / 2;
const y = 40;
// imageData must contain image data accepted by your jsPDF setup.
doc.addImage(imageData, "PNG", x, y, imageWidth, imageHeight);
doc.save("aligned-image.pdf");
The equation centers the image between the two margins, not necessarily on the physical page. With equal margins, those centers coincide; with unequal margins, the content-area center shifts. For left or right alignment within this same content area, use x = marginLeft or x = marginLeft + contentWidth - imageWidth.
Preserve proportions and fit an image into a box
Because addImage takes both width and height, arbitrary values can stretch or squash the source. If the source dimensions are iw by ih and you choose a target width tw, calculate th = tw * ih / iw. Then apply the alignment formula using tw and th, not the source pixel dimensions.
Contain: show the whole image
To fit the whole source inside a box of bw by bh without changing its aspect ratio, use the smaller scale factor:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallRank #3
- SET IT UP ONCE AND PRINT WITH CONFIDENCE. No complicated maintenance. Just easy, reliable printing you can count on.
- INK FOR YEARS. NOT MONTHS. Up to 2 years of ink included. Get thousands of pages of cartridge-free printing. More pages, less hassle
- KEEPS PRINTING WELL AFTER COMPETITORS HAVE QUIT. No complex maintenance. Sharper text, richer colors.[2] Only with HP Smart Tank
- PREMIUM SUPPORT - Strong technical expertise to solve issues faster
- THE LAST PRINTER YOU'LL EVER NEED. Enjoy years of refillable, cartridge-free printing.
const scale = Math.min(bw / iw, bh / ih);
const tw = iw * scale;
const th = ih * scale;
const x = left + (bw - tw) / 2;
const y = top + (bh - th) / 2;
doc.addImage(imageData, "PNG", x, y, tw, th);
This leaves unused space on one axis when the source and box have different aspect ratios. That is expected: contain preserves the complete image.
Cover: fill the box and crop
To fill the box while preserving proportions, use Math.max(bw / iw, bh / ih) instead. The resulting target rectangle exceeds the box along one axis, so part of it must be cropped. The inspected sources do not establish an automatic contain or cover mode in addImage. Crop the source image beforehand or use an intentional clipping approach appropriate to your PDF layout; simply passing the larger dimensions does not itself define a crop.
Align an image with another element
Use the same geometry when aligning an image to a heading, table, or other page element. If that element has a known left coordinate and width, treat those as left and boxWidth. If a preceding element determines the vertical position, use its bottom coordinate plus the desired gap as the image’s y. For repeated layouts, calculate the box once and derive every image coordinate from it, rather than accumulating offsets by eye.
- For left edges to match, use the same x-coordinate for the box and image.
- For centers to match, subtract half the image width from the other element’s center.
- For right edges to match, subtract the image width from the box’s right coordinate.
These are coordinate relationships, not special jsPDF layout constraints. If an element’s dimensions change, recalculate the dependent coordinates.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Rank #4
- Wireless Bluetooth Printer: Portable thermal printer compatible with iPhone, Android phones, iPad and tablet computers via Bluetooth. For smartphones, please download the "Nada Print" App. You can also connect to laptops and computers for printing using a USB-C cable. (Note: Laptops and computers can only be connected via USB and require the installation of a driver first. Bluetooth connection is not supported.)
- No-ink printing: Only supports US Letter and A4 size thermal paper.(Doesn't support regular paper) The no-ink portable thermal printer uses direct thermal technology, requiring no ink, toner or ribbons, making it environmentally friendly, cost-effective and time-saving. The thermal printer package comes with a roll of US Letter thermal printing paper. Note: When installing the paper, remember to switch the paper size switch on APP
- Clear Print: NDYIN N80 portable thermal printer adopts high-definition printing technology, with a 203DPI resolution to provide you with clear printing results. This mobile printer is compatible with roll paper, folded paper and tattoo transfer paper, supporting printing from your mobile phone PDF, Word, pictures and web pages anytime and anywhere. It is recommended to use our NDYIN thermal paper to achieve good printing quality
- Portable wireless printer for travel: The thermal printer is equipped with a built-in 1500mAh rechargeable battery, which can print 160 sheets of 8.5" x 11" thermal paper after being fully charged. It weighs only 1.5 pounds and is compact in size. This ink-free portable printer can be easily carried in a backpack or briefcase! It is perfect for business travel, cars, small offices, construction sites, schools and homes. You can print documents, contracts, invoices and boarding passes anytime and anywhere
- The N80 thermal printer has a wide range of uses. The package includes the N80 printer, a roll of US Letter paper(7m/roll), a user manual, a guide card, a type-C soft cable and a type C adapter. Note: The charging adapter is not included. Special thermal paper is required for use; ordinary paper cannot be used. This ink-free portable thermal printer is suitable for various scenarios such as home, school, travel, office, and outdoor, meeting the printing needs of different groups of people. This tattoo template printer is also compatible with tattoo transfer paper, making it an ideal choice for tattoo art
Check visual alignment, not just the coordinates
- Transparent padding: a PNG can include transparent pixels around its artwork. Its rectangular bounds may be centered correctly while the visible artwork looks shifted. Trim the asset or account for that internal padding in your design; the inspected jsPDF sources do not describe automatic transparent-pixel trimming.
- Rotation: the API declaration includes an optional rotation parameter. A rotated rectangle can extend beyond its unrotated bounds, so account for the rotated visual footprint when positioning it.
- Margins: distinguish page centering from content-area centering, especially when margins are asymmetric.
- Units: do not mix pixel dimensions with millimeter coordinates unless you have deliberately converted them.
Render or open the resulting PDF at the intended page size and inspect the actual asset. The jsPDF example demonstrates explicit sizing and rotation; it is not evidence that a particular layout has been tested.
Troubleshooting alignment problems
| Symptom | Likely cause | What to check or change |
|---|---|---|
| The image is consistently too far left or right | The calculation uses the page edge instead of the content box, or the margin is omitted. | Set the box’s left coordinate and width explicitly, including both margins, then recalculate x. |
| The image appears off-center despite a centered x-coordinate | Transparent border pixels shift the visible artwork inside the image rectangle. | Inspect the asset bounds; crop transparent padding or compensate for it deliberately. |
| The image looks stretched | The supplied target width and height do not match the source aspect ratio. | Derive one target dimension from the source ratio, then align that target rectangle. |
| The image fits in one direction but overflows in the other | The source and target box have different aspect ratios; contain and cover have different trade-offs. | Use the contain calculation to show all content, or a deliberate crop for cover. |
| A rotated image seems to cross its intended boundary | Positioning was calculated from unrotated width and height. | Re-evaluate the rotated bounding area and adjust its placement. |
| Coordinates that worked elsewhere are wrong in this document | The document’s configured unit or page size differs. | Check constructor options and use one base unit throughout; confirm the installed version’s page-size methods. |
Or skip the browser setup
If your actual input is a live webpage rather than an image file, ScreenshotNeo can capture the page as an image or PDF; it does not replace the jsPDF coordinate calculations above when you already have an image to place. One GET request returns a screenshot or PDF. See the ScreenshotNeo documentation for request options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo removes cookie and consent banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server provides screenshot and page-information tools for AI agents. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Learn more at ScreenshotNeo.
Sign up free for 1,000 screenshots a month, with no card required.
Frequently Asked Questions
Does jsPDF offer a built-in image alignment option?
The inspected current TypeScript declaration does not list a named image alignment flag; placement is specified through coordinates and dimensions.
Do the alignment formulas depend on a particular jsPDF release?
The formulas are basic rectangle geometry applied to the documented addImage arguments. Check the version installed in your project for its exact API and page-size methods.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




