【问题标题】:weird border/whitespace around image on printing html to pdf将html打印为pdf时图像周围的奇怪边框/空白
【发布时间】:2018-08-12 05:40:57
【问题描述】:

打印(输出 PDF)以下 html 时,图像周围有时会出现不需要的边框/空白。有时它出现在右侧,有时出现在左侧。它似乎只有一个像素宽。

我已经尝试过display: block; 无济于事。

如何去除图像周围的空白?

正文:

<body>
  <div class="doc">
    <div class="container-row">
      <div class="container leftSide">
        <div class="item">Item A</div>
        <img class="image" src="http://via.placeholder.com/350x450">
      </div>
    </div>
    <div class="container-row">
      <div class="container rightSide">
        <div class="item">Item A</div>
        <img class="image" src="http://via.placeholder.com/350x450">
      </div>
    </div>
    <!-- plus more "container-row"s -->
  </div>
</body>

css的相关部分:

@media print {
  .container-row {
    page-break-inside: avoid;
  }
}

body {
  background-color: transparent;
  margin: 0 !important;
  padding: 0 !important;
}

.doc {
  font-size: 0;
}

.container-row {
  display: inline-block; 
  width: 100%;
  border: 1px transparent solid; 
  font-size: 10px;
}

.container {
  max-width: 75%;
  word-wrap: break-word;
  margin: 10px 10px 0px 10px;
  overflow: hidden;
}

.image {
  width: 100%;
  margin: 5px 0px -5px;
}

.leftSide {
  float: left;
  border-top-left-radius: 13px;
  border-top-right-radius: 13px;
  border-bottom-right-radius: 13px;
}

.rightSide {
  float: right;
  border-top-left-radius: 13px;
  border-top-right-radius: 13px;
  border-bottom-left-radius: 13px;
}

.item {
  margin: 7px 10px 1px 10px;
}

.container p {
  margin-top: 0px;
  margin-left: 10px;
  margin-bottom: 7px;
  margin-right: 10px;
  padding: 0px;
}

【问题讨论】:

    标签: html css pdf printing


    【解决方案1】:

    通过将 image 类的 css 调整为:

    .image {
      width: 102%;
      margin: 5px -1% -5px;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2013-02-08
      相关资源
      最近更新 更多