Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Enhance Dockerfile and PDF report service for Puppeteer integration (#808)

Bill Yangcfc8b3735ea8
diff --git a/server/Dockerfile b/server/Dockerfile
index ac2495532..fb18f3b88 100644
--- a/server/Dockerfile
+++ b/server/Dockerfile
@@ -25,8 +25,19 @@ FROM node:20-alpine
WORKDIR /app
-# Install PostgreSQL client for migrations
-RUN apk add --no-cache postgresql-client
+# Install PostgreSQL client for migrations and Chromium for Puppeteer PDF generation
+RUN apk add --no-cache \
+ postgresql-client \
+ chromium \
+ nss \
+ freetype \
+ harfbuzz \
+ ca-certificates \
+ ttf-freefont
+
+# Configure Puppeteer to use system Chromium
+ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
+ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
# Copy built application and dependencies
COPY --from=builder /app/server/package*.json ./
diff --git a/server/src/services/pdfReports/pdfReportService.ts b/server/src/services/pdfReports/pdfReportService.ts
index 682ff6e56..a7f58fc61 100644
--- a/server/src/services/pdfReports/pdfReportService.ts
+++ b/server/src/services/pdfReports/pdfReportService.ts
@@ -464,7 +464,8 @@ class PdfReportService {
private async htmlToPdf(html: string): Promise<Buffer> {
const browser = await puppeteer.launch({
headless: true,
- args: ["--no-sandbox", "--disable-setuid-sandbox"],
+ executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
+ args: ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"],
});
try {
diff --git a/server/src/services/pdfReports/templates/PdfReportTemplate.tsx b/server/src/services/pdfReports/templates/PdfReportTemplate.tsx
index 3c5088304..e706a49aa 100644
--- a/server/src/services/pdfReports/templates/PdfReportTemplate.tsx
+++ b/server/src/services/pdfReports/templates/PdfReportTemplate.tsx
@@ -10,7 +10,8 @@ import {
formatDuration,
formatNumber,
safeToFixed,
- getCountryDisplay,
+ getCountryFlagUrl,
+ getCountryName,
formatDateRange,
} from "./components/index.js";
@@ -182,7 +183,9 @@ export const PdfReportTemplate = ({ reportData }: PdfReportTemplateProps): React
<TopListSection
title="Top Countries"
items={topCountries}
- renderLabel={item => getCountryDisplay(item.value)}
+ renderLabel={item => getCountryName(item.value)}
+ getIconUrl={item => getCountryFlagUrl(item.value)}
+ iconSize={{ width: 20, height: 15 }}
/>
}
right={<TopListSection title="Top Regions" items={topRegions} renderLabel={item => item.value} />}
diff --git a/server/src/services/pdfReports/templates/components/TopListSection.tsx b/server/src/services/pdfReports/templates/components/TopListSection.tsx
index 38814a595..f22b46359 100644
--- a/server/src/services/pdfReports/templates/components/TopListSection.tsx
+++ b/server/src/services/pdfReports/templates/components/TopListSection.tsx
@@ -7,10 +7,11 @@ interface TopListItemProps {
percentage: number | null;
count: number;
barWidth: number;
- favicon?: string;
+ iconUrl?: string;
+ iconSize?: { width: number; height: number };
}
-const TopListItem = ({ value, percentage, count, barWidth, favicon }: TopListItemProps) => (
+const TopListItem = ({ value, percentage, count, barWidth, iconUrl, iconSize }: TopListItemProps) => (
<div
style={{
display: "flex",
@@ -34,8 +35,14 @@ const TopListItem = ({ value, percentage, count, barWidth, favicon }: TopListIte
}}
/>
<div style={{ display: "flex", alignItems: "center", flex: "1", minWidth: "0", position: "relative", zIndex: 1 }}>
- {favicon && (
- <img src={favicon} alt="" width="16" height="16" style={{ marginRight: "8px", borderRadius: "2px" }} />
+ {iconUrl && (
+ <img
+ src={iconUrl}
+ alt=""
+ width={iconSize?.width ?? 16}
+ height={iconSize?.height ?? 16}
+ style={{ marginRight: "8px", borderRadius: "2px" }}
+ />
)}
<span
style={{
@@ -66,9 +73,18 @@ export interface TopListSectionProps {
items: MetricData[];
renderLabel: (item: MetricData) => string;
showFavicon?: boolean;
+ getIconUrl?: (item: MetricData) => string;
+ iconSize?: { width: number; height: number };
}
-export const TopListSection = ({ title, items, renderLabel, showFavicon }: TopListSectionProps) => {
+export const TopListSection = ({
+ title,
+ items,
+ renderLabel,
+ showFavicon,
+ getIconUrl,
+ iconSize,
+}: TopListSectionProps) => {
if (items.length === 0) return null;
const ratio = items[0]?.percentage ? 100 / items[0].percentage : 1;
@@ -86,7 +102,12 @@ export const TopListSection = ({ title, items, renderLabel, showFavicon }: TopLi
<div style={{ color: "#111827", fontSize: "14px", fontWeight: "600", marginBottom: "12px" }}>{title}</div>
{items.map((item, index) => {
const barWidth = (item.percentage ?? 0) * ratio;
- const favicon = showFavicon ? `https://www.google.com/s2/favicons?domain=${item.value}&sz=16` : undefined;
+ let iconUrl: string | undefined;
+ if (getIconUrl) {
+ iconUrl = getIconUrl(item);
+ } else if (showFavicon) {
+ iconUrl = `https://www.google.com/s2/favicons?domain=${item.value}&sz=16`;
+ }
return (
<TopListItem
@@ -95,7 +116,8 @@ export const TopListSection = ({ title, items, renderLabel, showFavicon }: TopLi
percentage={item.percentage}
count={item.count}
barWidth={barWidth}
- favicon={favicon}
+ iconUrl={iconUrl}
+ iconSize={iconSize}
/>
);
})}
diff --git a/server/src/services/pdfReports/templates/components/index.ts b/server/src/services/pdfReports/templates/components/index.ts
index 1b44cb3d9..3dc379108 100644
--- a/server/src/services/pdfReports/templates/components/index.ts
+++ b/server/src/services/pdfReports/templates/components/index.ts
@@ -8,7 +8,7 @@ export {
formatNumber,
safeToFixed,
truncateString,
- getCountryFlag,
- getCountryDisplay,
+ getCountryFlagUrl,
+ getCountryName,
formatDateRange,
} from "./utils.js";
diff --git a/server/src/services/pdfReports/templates/components/utils.ts b/server/src/services/pdfReports/templates/components/utils.ts
index 1f0d58591..20a08cedd 100644
--- a/server/src/services/pdfReports/templates/components/utils.ts
+++ b/server/src/services/pdfReports/templates/components/utils.ts
@@ -42,20 +42,14 @@ export const truncateString = (str: string, maxLength: number = 35): string => {
const regionNamesInEnglish = new Intl.DisplayNames(["en"], { type: "region" });
-export const getCountryFlag = (countryCode: string): string => {
+export const getCountryFlagUrl = (countryCode: string): string => {
if (!countryCode || countryCode.length !== 2) return "";
- const codePoints = countryCode
- .toUpperCase()
- .split("")
- .map(char => 127397 + char.charCodeAt(0));
- return String.fromCodePoint(...codePoints);
+ return `https://flagcdn.com/w20/${countryCode.toLowerCase()}.png`;
};
-export const getCountryDisplay = (countryCode: string): string => {
+export const getCountryName = (countryCode: string): string => {
try {
- const flag = getCountryFlag(countryCode);
- const name = regionNamesInEnglish.of(countryCode.toUpperCase()) || countryCode;
- return `${flag} ${name}`;
+ return regionNamesInEnglish.of(countryCode.toUpperCase()) || countryCode;
} catch {
return countryCode;
}