Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rgba is not supported #966

Closed
LuoHpeng opened this issue Mar 18, 2024 · 5 comments
Closed

rgba is not supported #966

LuoHpeng opened this issue Mar 18, 2024 · 5 comments

Comments

@LuoHpeng
Copy link

"<meta charset="UTF-8">"
+ "<h1 style ="font-family: 'BabelStoneHan',sans-serif;">中文HTML转PDF示例"
+ "

这是一个包含中文的 HTML 内容。


"
+ "<table style="width:100%; border-collapse: collapse;font-family: 'BabelStoneHan',sans-serif;">"
+ "<th style="border: 1px solid rgb(10, 18, 10); padding: 8px;font-family: 'BabelStoneHan',sans-serif;">表头1"
+ "<th style="border: 1px solid red; padding: 8px;">表头2"
+ "<th style="border: 1px solid rgba(0, 0, 0, 0.06); padding: 8px;">表头3"
+ "<td style="border: 1px solid black; padding: 8px;">数据1"
+ "<td style="border: 1px solid black; padding: 8px;">数据2"
+ "<td style="border: 1px solid black; padding: 8px;">数据3"
+ "";

rgba(0,0,0,0.06,) is not a border width, style, or color at line 98. Skipping declaration.

@LuoHpeng LuoHpeng changed the title rgba not support rgba is not supported Mar 18, 2024
@LuoHpeng
Copy link
Author

WechatIMG33126

@siegelzc
Copy link

openhtmltopdf does not support opacity

it looks like you may be using the alpha channel here as an analogue for HSL-style "lightness". you can achieve this effect by rendering the html as you would expect in a browser, and then using an eyedropper tool to get the RGB value of the rendered color

@siegelzc
Copy link

siegelzc commented Mar 20, 2024

BTW, the community is in the process of transitioning to a new repo at https://github.com/openhtmltopdf/openhtmltopdf because danfickle is no longer maintaining this repository.

@LuoHpeng
Copy link
Author

The following method converts rgba in html to rgb

public static final Color BACK_GROUND = new Color(255, 255, 255);

public static final String RGBA_REGEX = "rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)";

public static final Pattern PATTERN = Pattern.compile(RGBA_REGEX);

public static String rgbaToRgb(String htmlString) {

Matcher matcher = PATTERN.matcher(htmlString);

// Replace the matching RGBA color to RGB format
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
  int red = Integer.parseInt(matcher.group(1));
  int green = Integer.parseInt(matcher.group(2));
  int blue = Integer.parseInt(matcher.group(3));
  // alpha
  float alpha = Float.parseFloat(matcher.group(4));
  red = (int) (red * alpha + BACK_GROUND.getRed() * (1 - alpha));
  green = (int) (green * alpha + BACK_GROUND.getGreen() * (1 - alpha));
  blue = (int) (blue * alpha + BACK_GROUND.getBlue() * (1 - alpha));
  // Convert RGBA to RGB
  String rgbColor = "rgb(" + red + ", " + green + ", " + blue + ")";
  matcher.appendReplacement(sb, rgbColor);
}
matcher.appendTail(sb);
return sb.toString();

}

@siegelzc
Copy link

Feel free to open an issue at the other repo with this code snippet. It would be cool if we could add transparency support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants