Pagedown (R) 生成的 PDF 报告中的数字未居中

问题描述 投票:0回答:1

我正在尝试使用 pagedown 生成 PDF 报告。生成的报告包含不在页面中心的图像。这是 RMD 文件:

---
title: "`r params$name`"
author: "`r params$school`"
date: "`r Sys.Date()`"
params:
  name: "XYZ"
  school: "ABC School"
output:
  pagedown::html_paged:
    css: style.css
knit: pagedown::chrome_print
---
knitr::opts_chunk$set(echo = TRUE)


packages <- c("tidyverse")
#lapply(packages, install.packages, character.only = TRUE) 
lapply(packages, library, character.only = TRUE) 


anxiety_value <- 12
min_score_anxiety <- 7
max_score_anxiety <- 35

basic_seq <- seq(min_score_anxiety, max_score_anxiety, by = 4)
final_seq <- unique(c(basic_seq, anxiety_value, max_score_anxiety))
final_seq <- sort(final_seq)



data.frame(min = min_score_anxiety, value = anxiety_value, max = max_score_anxiety) |>
  ggplot(aes(y = 1)) +
  geom_linerange(aes(xmin = min, xmax = max), lwd = 100, color = "salmon") +
  geom_vline(aes(xintercept = value), lwd = 10, alpha = 0.5) +
  coord_cartesian(ylim = c(0, 2), expand = FALSE) +
  scale_x_continuous(breaks = final_seq) +
  ggtitle(paste("Anxiety Score for", params$name)) +
  labs(y = "") + 
  theme_void(base_size = 25) +
  #theme_Publication() + 
  theme(aspect.ratio = 1/5,
        axis.ticks.x = element_line(),
        axis.ticks.length.x = unit(5, "mm"),
        axis.text.x = element_text(),
        axis.line.x = element_line(),
        axis.line.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(), 
        plot.title = element_text(hjust = 0.5, margin = margin(20, 20, 20, 20)),
        plot.margin = margin(20, 20, 20, 20))

这是 style.css:

@page {
  size: letter;
  margin: 1in;
  background-color: papayawhip;

  @bottom-left-corner {
    content: ""; 
    background-color: black;
    height: 10px;  
    padding: 50px;  
  }

  @bottom-center {
    content: ""; 
    background-color: black;    
  }
  
  @bottom-right-corner {
    background-color: black;    
    content: counter(page);
    color: whitesmoke;
    font-size: 20pt;
  }
}

@page:first{
  background-color: #ffffff;
  background-image: url(Women_Cover_Page.png);
  background-size: cover;
  background-repeat: no-repeat;

  @top-left-corner {
    display: none;
  }

  @top-left {
    display: none;
  }

  @top-center {
    display: none;
  }

  @top-right {
    display: none;
  }

  @top-right-corner {
    display: none;
  }

  @bottom-right {
    display: none;
  }

  @bottom-left-corner {
    display: none;  
  }

  @bottom-left {
    display: none;
  }  

  @bottom-center {
    display: none;    
  }
  
  @bottom-right {
    display: none;    
  }

  @bottom-right-corner {
    display: none;
  }

}

/* img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
} */


:root {
  --background: papayawhip, whitesmoke;
  --pagedjs-width: 210mm;
  --pagedjs-height: 297mm;
  --color-paper: white;
  --color-mbox: rgba(0, 0, 0, 0.2);
  --running-title-width: 2.5in;
  --screen-pages-spacing: 3mm;
  --main-color: rgb(114, 76, 37);
  --secondary-color: #346aa0;
  --main-font: "Roca Two";
  --header-font: "Lato","Inter";
}

p, ul, a, ol {
  font-family: var(--main-font);
}

p {
  font-size: 18pt;
}

h1, h2, h3, h4 {
  font-family: var(--header-font);
}

h1 {
  font-size: 36pt;
  color: #093757;
  text-align: left;
  background-repeat: no-repeat, no-repeat;
  background-position: 5% 40%, 95% 40%;
  background-size:  5cm, 5cm;
  
}

h1.title {
  font-size: 36pt;
  font-family: Roca Two, Alta, sans-serif;
  font-style: bold;
  top: 90px;
  left: 80px;
  margin-left: 2%;
  margin-top: 30%;
  font-weight: 900;
  padding-bottom: 0;
  padding-top: 0;
  background: transparent;
}


h2 {
  font-size: 24pt;
  font-family: Roca Two, Alta, sans-serif;
  font-style: bold;
  margin-left: -2%;
  background: transparent;
}

h2.title {
  font-size: 24pt;
  font-family: Roca Two, Alta, sans-serif;
  font-style: bold;
  background: transparent;
}

输出(仅粘贴图表)

我尝试过以下方法:

  • fig.align
    属性不执行任何操作。我用了“左”,但数字保持不变。

  • 我减少了 .css 文件中的边距,这会将图像向左移动一点,但我只想将图像居中。即,左侧和右侧的填充应该相同。不知道该怎么做。

css r-markdown quarto pagedown
1个回答
0
投票

我能够使用 css 解决这个问题。

img {
  width: 100%;
}

解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.